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)
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.
Joined: May 26, 2005
It returns the title of the file, CarParts.txt [ November 22, 2005: Message edited by: Stephanie Dears ]
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.
Joined: May 26, 2005
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.
Joined: Aug 16, 2005
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 ]