473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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:

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
5 2297
myusernotyours
188 100+
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
r035198x
13,262 8TB
@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
9,735 Expert Mod 8TB
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
pbrockway2
151 Expert 100+
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
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

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

Similar topics

2
by: greg | last post by:
heres my text file(well the first two lines of it) ######################################### # Default BMUS server CFG file # this file is written with the...
1
by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
2
by: Jean-Marie Vaneskahian | last post by:
Reading - Parsing Records From An LDAP LDIF File In .Net? I am in need of a .Net class that will allow for the parsing of a LDAP LDIF file. An LDIF file is the standard format for representing...
1
by: Thomas Kowalski | last post by:
Hi, I have to parse a plain, ascii text file (on local HD). Since the file might be many millions lines long I want to improve the efficiency of my parsing process. The resulting data structure...
1
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from...
3
by: Cuong.Tong | last post by:
Greeting, I am writing my own web server and having some problme parsing the the mulitpart/form-data stream that is sent from the browsers. I have a form looks something like this <form...
4
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
3
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.