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: -
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.
5 2297
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.
@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.
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: -
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
- 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.
Thank you all for the help.
I had tried: - 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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |