Connecting Tech Pros Worldwide Help | Site Map

Trouble parsing int while reading a file

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: 4 Weeks Ago
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:

Expand|Select|Wrap|Line Numbers
  1. String input = "";
  2.  
  3. while(readFile.hasNextLine()) 
  4. {
  5.      input = readFile.nextLine();
  6.      String sName = input.substring(0, 14);
  7.      String sCap = input.substring(15,29);
  8.      String sAbbr = input.substring(30,32);
  9.      int sPop = Integer.parseInt(input.substring(32,40)); // breaks here
  10.      String sReg = input.substring(40,55);
  11.      int sRegNum = Integer.parseInt(input.substring(55,56));
  12.  
  13.      sArray.insert(sName, sCap, sAbbr, sPop, sReg, sRegNum);
  14.  
  15. }// end while
  16.  
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's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#2: 4 Weeks Ago

re: Trouble parsing int while reading a file


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.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: 4 Weeks Ago

re: Trouble parsing int while reading a file


Quote:

Originally Posted by javanianewbie View Post

.... 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.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#4: 4 Weeks Ago

re: Trouble parsing int while reading a file


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:
Expand|Select|Wrap|Line Numbers
  1. String input = "";
  2.  
  3. while(readFile.hasNextLine()) 
  4. {
  5.      input = readFile.nextLine();
  6.      String sName = input.substring(0, 14);
  7.      String sCap = input.substring(15,29);
  8.      String sAbbr = input.substring(30,32);
  9.      int sPop;
  10.      try{
  11.        sPop = Integer.parseInt(input.substring(32,40).trim()); // breaks here
  12.      }catch(NumberFormatException e) {
  13.        sPop = 0;
  14.     }
  15.  
  16.      String sReg = input.substring(40,55);
  17.      int sRegNum;
  18.      try{
  19.        sRegNum= Integer.parseInt(input.substring(55,56).trim());
  20.      }catch(NumberFormatException e) {
  21.        sRegNum = 0;
  22.      }
  23.  
  24.      sArray.insert(sName, sCap, sAbbr, sPop, sReg, sRegNum);
  25.  
  26. }// end while
Newbie
 
Join Date: Nov 2007
Posts: 27
#5: 4 Weeks Ago

re: Trouble parsing int while reading a file


Expand|Select|Wrap|Line Numbers
  1. try {
  2.     sPop = Integer.parseInt(input.substring(32,40).trim()); // breaks here
  3. } catch(NumberFormatException e) {
  4.     sPop = 0;
  5. }
  6.  
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.
Newbie
 
Join Date: Oct 2009
Posts: 5
#6: 4 Weeks Ago

re: Trouble parsing int while reading a file


Thank you all for the help.

I had tried:

Expand|Select|Wrap|Line Numbers
  1. 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!
Reply


Similar Java bytes