473,403 Members | 2,323 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,403 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 2299
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.