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 2316
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.
Frinavale 9,735
Recognized Expert Moderator Expert
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 following code
fi = open('bmus.cfg', 'w')
print 'file opened'
cfg =
|
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 the application's front end the file appears to be corrupt. The front-end application has a way of inserting files to the column, however when I analyze this in SQL Profiler the contents of the Byte array appear to be quite different than the one...
|
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 LDAP
objects. I need to be able to read the records from an LDIF file into
..Net.
There exists a Perl module that will do exactly this called
Net::LDAP::LDIF but I am wanting to port my code over to .Net and
cannot find anything with similar...
|
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 shall look like this
the following:
class A {
...
int value;
}
|
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 the file.......
now i have to convert this in decimal and save in an array.of integers.......i thought it can be achieved through parsing ..means 0f could be stored in array converted in decimal...but remmber i m reading from a file///////////////i...
| |
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 action="process.dll>
|
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 what I'm trying to do is upload a html file onto the web
server, convert it to aspx file and then parse it for input
tags/controls, which in turn will become fields in a newly created
database table.
Clearly when the aspx file is called the...
|
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 the file) with the location.
And for a particular section I parse only that section.
The file is something like,
.... DATAS
|
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 for the most part, but
the problem is when attempting to continuously pipe information into
the application via the tail -f command. The command line looks
something like this:
tail -f <logfile| grep <search string| python parse.py
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |