Results 1 to 6 of 6
Thread: Problem with string parsing.
- 08-31-2010, 03:14 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
[SOLVED]Problem with string parsing.
Hello,
I am working on a project for school where we need to open zip file, read content of each file within zip (which are all text files) and parse string from those files. I need to isolate name and integer corresponding to each name. I will paste code now here, and then tell you what is the problem.
The program throws an NumberFormatException. Now, I know what the problem is, but I can't figure out why or how to fix it. Integer.ParseInt() tries to convert a string that does not have the appropriate format. I'm guessing the problem is with delimiters. Text in the file looks consistent. I will attach exact text file I am testing on. If someone would please test this for me and point me in the right direction for solving this problem.Java Code:public class StringParser { private final int NAME_INDEX = 0; private final int WEIGHT_INDEX = 2; private String inputLine = ""; private String name = ""; private int weight; public StringParser(String zipReadList) { inputLine = zipReadList; } public void parseString() { Scanner input = new Scanner(inputLine).useDelimiter("\n"); try { while (input.hasNext()) { String temp = input.next(); String[] splitInputLine = temp.split(","); name = splitInputLine[NAME_INDEX]; weight = Integer.parseInt(splitInputLine[WEIGHT_INDEX]); System.out.println(name + " " + weight); } } catch (NumberFormatException e) { e.printStackTrace(); } } }Last edited by dc0m; 08-31-2010 at 06:25 PM.
- 08-31-2010, 03:30 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Hm, your code seems to work well for me with the text file that you've given us.
- 08-31-2010, 03:48 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
Thanks for your fast response.
Hm, that is weird. I've tested the same code with same file on school and home computersm producing same error. Well, I'm glad at least it worked for somebody. =)
This is where my stops.
It looks like it goes back to Anna, which is at the beginning, so the format becomes name,name,gender,numberJava Code:Lillian 672 Ada 652 Lillie 647 Helen 636 Jessie 635 Louise 635 Ethel 633 Lula 621 Myrtle 615 java.lang.NumberFormatException: For input string: "Anna" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Integer.parseInt(Integer.java:499) at StringParser.parseString(StringParser.java:36) at NamesWeight.main(NamesWeight.java:13)
Last edited by dc0m; 08-31-2010 at 03:52 AM.
- 08-31-2010, 03:54 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Curious as Myrtle's not even at the end of the file. Perhaps there's an issue with how you're reading the zip file. First thing I would do is to read the text files from the Zip file and print them out as I was reading them, to make sure that everything's being read in correctly. Then I'd do the same with the String that I'm feeding into the StringParser program to see that it is well formed. The key is to keep probing until the source of the error is revealed. Once found that's more than half the battle. If this doesn't help, then perhaps Norm will have something good to add. He's one of the best intuitive debuggers around here.
Last edited by curmudgeon; 08-31-2010 at 03:57 AM.
- 08-31-2010, 04:29 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
I did all that already. I print them out as I was reading them. Also, I was concatenating all that to one string, and I've printed out that generated string before it's passed to StringParser. And of course at the end I've printed it out within the StringParser (also worked). =/
Thanks again.
Here is debugger screenshot. You can actually see how the splitInputLine looks like when it fails.
Last edited by dc0m; 08-31-2010 at 04:54 AM. Reason: adding debugger screenshot
- 08-31-2010, 06:25 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
problems with parsing a string into int,
By bigj in forum New To JavaReplies: 3Last Post: 01-06-2010, 10:32 PM -
Parsing string and simple calculation
By sapina007 in forum Advanced JavaReplies: 4Last Post: 08-21-2009, 12:07 PM -
Multi Delimeter String Parsing
By yuriyl in forum Advanced JavaReplies: 5Last Post: 07-13-2009, 03:34 PM -
parsing numbers in a string
By rsoler in forum Advanced JavaReplies: 4Last Post: 03-31-2009, 06:05 AM -
parsing problem
By Pooja Deshpande in forum Advanced JavaReplies: 4Last Post: 05-29-2008, 01:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks