Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes string tokenizer, reading from .txt file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string tokenizer, reading from .txt file" Watch "string tokenizer, reading from .txt file" New topic
Author

string tokenizer, reading from .txt file

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
I'm trying to read a string from a .txt file. It compiles, but when I input the file name, I get an error.

C:\cm241_work>java CarPartProcessing
File: CarParts.txt
Exception in thread "main" java.lang.NumberFormatException: For input string: "CarParts.txt"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at CarPartProcessing.main(CarPartProcessing.java:43)

Here's my code:

import java.io.* ;
import java.util.*;
import java.lang.*;

public class CarPartProcessing {

public static void main (String [] args) throws IOException {

final int MAX_LENGTH = 30;
CarPart[] array = new CarPart[MAX_LENGTH];

int id;
String name;
int stock;
double price;

Scanner stdin = new Scanner (System.in);
System.out.print("File: ");
String fileName = stdin.nextLine();

String st = new String (fileName);
StringTokenizer tokens = new StringTokenizer(st);

while(tokens.hasMoreTokens()){

id = Integer.parseInt(tokens.nextToken());
name = tokens.nextToken();
stock = Integer.parseInt(tokens.nextToken());
price = Double.parseDouble(tokens.nextToken());

System.out.println(id);
System.out.println(name);
System.out.println(stock);
System.out.println(price);
} //end while
}
}
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
Do this; Right before the line id = Integer.parseInt(tokens.nextToken()); add a System.out.pringln(tokens.nextToken()), and then comment out everything after that using /* */

This way you will be able to see what is that you are actually trying to parse to an int. My guess is that it either isn't a digit like you think it is, or there are some whitespace characters that need to be removed first (I don't remember if tokenizer removes them or not.)

Either way, you will be able to see what is going on, and can probably figure out what to do next.
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
It returns the title of the file, CarParts.txt
[ November 22, 2005: Message edited by: Stephanie Dears ]
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
Stephanie,
You have tokenized the file name and trying to parse the tokens into integer.

You typed filename in stdin using keyboard. The variable filename contains the value CarParts.txt



You are expecting st to contain the contents of file CarParts.txt. "new String" just creates another String. It don't read the file.

One way to read is here
http://www.devdaily.com/java/edu/pj/pj010004/pj010004.shtml
It is using jdk1.0
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
The same thing happens. If I do what you said in your first post, I get the name of the text file. Otherwise I get the errors I got before.
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
Ok, let's go step by step and see if we can help you see the problem. As you already figured out, the error is being caused because you are trying to turn a string of letters into an int.

Here is a link to the API for StringTokenizer:
StringTokenizer

Notice that it says

The string tokenizer class allows an application to break a string into tokens


It doesn't say anything about being able to read in from a text file (which it looks like you are trying to do). If you look a t the sample code given there, it tells you almost exactly what you are doing. As jiju ka pointed out, all that you are doing is tokenizing the file name.

Try this, and I think you will understand. Change this line of code:
String st = new String (fileName);
to:
String st = new String ("2 radiator 156 12.50");

Then rerun it and see what happens. It should work. That is because you now have a string that the tokenizer can work with, as well as your parse methods.

What you are really trying to do is read in a line of text from a file, and that is something completely different. Here is a fairly simple example of what you might want to try and do:
Reading text from a file

You are going to get introduced to Try/Catch statements, and some minor exception handling which can be a bit intimidating at first though.

See if that helps.
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
You said: Try this, and I think you will understand. Change this line of code:
String st = new String (fileName);
to:
String st = new String ("2 radiator 156 12.50");

I can't do that. My assignment says it has to read it from the file.
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
What I am trying to show you is that if you get the proper input, then it will work. By temporarily changing that line of code, I was trying to show you what type of input you needed. You have to read the text from a file, and as far as I know, StringTokenizer is not able to do that. What I am trying to show you is a way to chase down errors in your code so that you can find out what is going on at any particular point.

You need to read a line of text from your file, and then let the StringTokenizer handle it. Once you do that, you will have it made.
[ November 22, 2005: Message edited by: C Clites ]
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

1. String fileName = stdin.nextLine();
2. String st = new String (fileName);
3. StringTokenizer tokens = new StringTokenizer(st);

Ok, now to get what you want we will have to add some lines of code between the first and third lines.

what is being currently done is you are reading in the name of file which contains the data which you want to tokenize.

The first line "String fileName = stdin.nextLine();" gets the file name for you, but the second line does not work the way you expect it to.

what you have to try to do is use the "fileName" to construct a file and read from it.

" 2. String st = [read from file, whose name you have got from the first line]"

Have a look at java.io package and it will help you with the file stuff.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: string tokenizer, reading from .txt file
 
Similar Threads
Exit this Do-While w/out an int?
sorting
Need a little help...
extracting strings
Scanner is skipping