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!

[2779 byte] By [Karasa] at [2007-10-3 3:05:25]
# 1

Hi,

When you write a line to the file it appears you are writing it in a set manner. You should then use your delimiters to determine each element. This way you don't need to worry whether a string is 1 or 2 words, it'll only split on the character you tell it to.

You could use a # for example toolname#quantity#price

You can then use the String split() method to get each element.

String [] fileArr = line.split("#");

String toolname = fileArr[1];

int quantity = Integer.parseInt(fileArr[2]);

double price = Double.parseDouble(fileArr[3]);

You shouldn't need a try and catch block for a NumberFormatException if you know what you are writing to file.

Just as an aside could you please post your code in the code tags provided (next to the spell check button).

HTH,

Chris

lordflashearta at 2007-7-14 20:55:30 > top of Java-index,Java Essentials,New To Java...