By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,501 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,501 IT Pros & Developers. It's quick & easy.

Trouble parsing int while reading a file

P: 5
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.
Oct 20 '09 #1
Share this Question
Share on Google+
5 Replies


myusernotyours
100+
P: 188
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.
Oct 20 '09 #2

10K+
P: 13,264
@javanianewbie
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.
Oct 20 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
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
Oct 20 '09 #4

Expert 100+
P: 151
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.
Oct 20 '09 #5

P: 5
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!
Oct 21 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.