Strings - problem with reading double words....
Hi...
Im currently storing data to file like this....
System.out.print(" Enter Tool Name: ");
String s = scan.nextLine();
System.out.print(" Enter Quantity: ");
int s1 = scan.nextInt();
System.out.print(" Enter Price ($A): ");
float d = scan.nextFloat();
scan.nextLine();
while(d <= 0.00D)
{
System.out.print(" Error - enter a price greater than zero: ");
d = scan.nextFloat();
scan.nextLine();
}
.......
System.out.println(" Writing record to file " + fileName);
outputStream.println(" " + count + "\t\t" + pad(s ,20) + s1 + "\t\t" + d);
outputStream.close();
System.out.println(" Writing record complete...");
}
This works fine....my main problem is when Im reading the file with a name comprised of two words...like "Electric Sander"..it reads fine for singular. and again I can make it work fine for two words but if its one it returns an error....
This is what Im using...
while (inputStream.hasNextLine())
{
line = inputStream.nextLine();
tokenizer = new StringTokenizer (line);
RecNum = tokenizer.nextToken();
ToolName = tokenizer.nextToken();
try
{
Quantity = Integer.parseInt(tokenizer.nextToken());
Price = Float.parseFloat (tokenizer.nextToken());
items[count++] = new ListToolDetails (RecNum,ToolName,Quantity, Price);
}
catch (NumberFormatException exception)
{
System.out.println (" Error in input. Line ignored!");
return;
}
}
for (int scan = 0; scan < count; scan++)
System.out.println (items[scan]);
with a ToString method:
public String toString()
{
String s = "";
s = (new StringBuilder()).append(s).append(" ").toString();
s = (new StringBuilder()).append(s).append(exactLength(RecNum, 4)).toString();
s = (new StringBuilder()).append(s).append("").toString();
s = (new StringBuilder()).append(s).append(exactLength(ToolName, 20)).toString();
s = (new StringBuilder()).append(s).append(" ").toString();
DecimalFormat numItems = new DecimalFormat(" ");
s = (new StringBuilder()).append(s).append(exactLength(numItems.format(Quantity),9)).toString();
s = (new StringBuilder()).append(s).append(" ").toString();
s = (new StringBuilder()).append(s).append("").toString();
s = (new StringBuilder()).append(s).append(exactLength(fmt.format(Price), 13)).toString();
s = (new StringBuilder()).append(s).append(" ").toString();
return s + " " + "\t" + fmt.format((Quantity * Price));
}
can some please advise how I can fix this? Im sure its easy but I just cant seem to make it work!

