Trouble parsing int while reading a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javanianewbie
    New Member
    • Oct 2009
    • 5

    Trouble parsing int while reading a file

    Hello,

    I am having trouble when I read in a a file and parsing part of it as an int using substring.

    Here is the code I have:

    Code:
    String input = "";
    
    while(readFile.hasNextLine()) 
    {
         input = readFile.nextLine();
         String sName = input.substring(0, 14);
         String sCap = input.substring(15,29);
         String sAbbr = input.substring(30,32);
         int sPop = Integer.parseInt(input.substring(32,40)); // breaks here
         String sReg = input.substring(40,55);
         int sRegNum = Integer.parseInt(input.substring(55,56));
    
         sArray.insert(sName, sCap, sAbbr, sPop, sReg, sRegNum);
                        
    }// end while
    Basically I cant read in sPop. I know this is due to the fact that on some parts of the file there is white space in spaces 32-34. Im stumped on how to trim or omit this white space so I don't get an error. I have tried using .trim() but it doesn't work with integers.

    Any help on this would be great. I have been searching on how to do this but cant seem to put it all together.

    Thanks.
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    It's always a good idea to verify your assumptions in the code.
    For example, why do you assume that what you are passing to parseInt is actually an integer. Or even that there is something in the string at position 32 or wherever.
    What if the string is just 20 characters long?

    Regards,

    Alex.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by javanianewbie
      .... I have tried using .trim() but it doesn't work with integers.

      ...
      You trim a String to remove leading and or trailing spaces. It doesn't really make sense to want to trim an integer. Trim the string before passing it to parseInt.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        r035198x's suggestion will remove leading/trailing white spaces.
        I would take this one step further and place a try/catch block around it to catch the cases the input is not a number.

        For example:
        Code:
        String input = "";
         
        while(readFile.hasNextLine()) 
        {
             input = readFile.nextLine();
             String sName = input.substring(0, 14);
             String sCap = input.substring(15,29);
             String sAbbr = input.substring(30,32);
             int sPop;
             try{
               sPop = Integer.parseInt(input.substring(32,40).trim()); // breaks here
             }catch(NumberFormatException e) {
               sPop = 0;
            }
        
             String sReg = input.substring(40,55);
             int sRegNum;
             try{
               sRegNum= Integer.parseInt(input.substring(55,56).trim());
             }catch(NumberFormatException e) {
               sRegNum = 0;
             }
         
             sArray.insert(sName, sCap, sAbbr, sPop, sReg, sRegNum);
         
        }// end while

        Comment

        • pbrockway2
          Recognized Expert New Member
          • Nov 2007
          • 151

          #5
          Code:
          try {
              sPop = Integer.parseInt(input.substring(32,40).trim()); // breaks here
          } catch(NumberFormatException e) {
              sPop = 0;
          }
          Catching the exception is good but instead of assigning sPop a bogus value you could do whatever it is you intend to do.

          In your original post you say that there might be white space in the substring. What is supposed to happen in that case?

          If the sArray.insert() is not supposed to happen you could check that the NFE occurred because of white space and, if so, just "continue".

          If the NFE occurred for any other reason, or if the white space in this part of the input is an actual error, then it might be better for the method to throw the exception rather than put bogus values into the array.

          Comment

          • javanianewbie
            New Member
            • Oct 2009
            • 5

            #6
            Thank you all for the help.

            I had tried:

            Code:
            sPop = Integer.parseInt(input.substring(32,40).trim()); // breaks here
            And it wasnt working. I now realize this was due to the fact that in some parts of the file it wasnt finding ints and was breaking. Now with the try/catch Im up and running. I do however, want to improve on this and mess around with pbrockway's suggestion.

            Once again thank you all for the help and the jump-start to my brain!

            Comment

            Working...