Results 1 to 6 of 6
  1. #1
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default [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.

    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();		    
    		}
    	}
    }
    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.
    Attached Files Attached Files
    Last edited by dc0m; 08-31-2010 at 07:25 PM.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Hm, your code seems to work well for me with the text file that you've given us.

  3. #3
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    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.
    Java 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)
    It looks like it goes back to Anna, which is at the beginning, so the format becomes name,name,gender,number
    Last edited by dc0m; 08-31-2010 at 04:52 AM.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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 04:57 AM.

  5. #5
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    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 05:54 AM. Reason: adding debugger screenshot

  6. #6
    dc0m is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    Problem Solved. I've rewritten StringParser and ReadZip classes. Now, everything works.

Similar Threads

  1. problems with parsing a string into int,
    By bigj in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 11:32 PM
  2. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 01:07 PM
  3. Multi Delimeter String Parsing
    By yuriyl in forum Advanced Java
    Replies: 5
    Last Post: 07-13-2009, 04:34 PM
  4. parsing numbers in a string
    By rsoler in forum Advanced Java
    Replies: 4
    Last Post: 03-31-2009, 07:05 AM
  5. parsing problem
    By Pooja Deshpande in forum Advanced Java
    Replies: 4
    Last Post: 05-29-2008, 02:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •