Results 1 to 13 of 13
Thread: read integers from txt
- 05-27-2010, 02:23 PM #1
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
read integers from txt
Hi,
I have a txt file like this
I have to read the Integers to use them further in the program, but I always get an error like thisJava Code:18 1200 18 1600 18 2400 18 1100
I don't understand where the symbols come from nor how to ignore them.Exception in thread "main" java.lang.NumberFormatException: For input string: "18"
First I read the file line by line as a String and then I would convert the strings into integers with a simple parseInt-instruction. I thought this could be done to the whole line because there are only Integers in the file.
My code so far is shown below. To understand the code --> 'leeftijden' is dutch for ages ;-)
I throw the exceptions because this is just a small exercise for school whithout any further use of the code. I thought this would be really easy, but I don't see my fault.Java Code:public Enquete() throws NumberFormatException, IOException { leeftijden = new Hashtable[65-18]; // lonen inlezen BufferedReader r = new BufferedReader(new FileReader("lonen.txt")); while((s = r.readLine()) != null){ setLoon(Integer.parseInt(s),Integer.parseInt(r.readLine())); } //System.out.println(leeftijden[0].toString()); }
Thanks,
Genji
- 05-27-2010, 03:37 PM #2
Are you sure that the file you're reading from is ASCII and not UNICODE?
Break your compound statement up into parts to see what is happening.
Use println() to display values before trying to convert them to ints.setLoon(Integer.parseInt(s),Integer.parseInt(r.rea dLine())
- 05-27-2010, 04:23 PM #3
Might be unicode or some other format..check the file format.
Last edited by RamyaSivakanth; 05-27-2010 at 04:34 PM.
Ramya:cool:
- 05-27-2010, 05:20 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Use this to check if you have problem with unicode characters:
Java Code:import java.io.FileNotFoundException; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; public class ReadIntegers { public static void main(String[] args) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader("file.txt")); String currentLine = ""; while((currentLine = reader.readLine()) != null) { int number = Integer.parseInt(currentLine); System.out.println(number); } } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
- 05-27-2010, 05:48 PM #5
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Hi
I don't understand why this can be a unicode problem.
When I change the file to(inserting a white line at the beginning)
18
1200
18
1600
18
2400
18
1100
will solve the problem on the first line. I don't understand why there's only a problem on the first line.
I changed my code as follows to debug easily:
This gives the following result:Java Code:public Enquete() throws NumberFormatException, IOException { leeftijden = new Hashtable[65-18]; // lonen inlezen BufferedReader r = new BufferedReader(new FileReader("lonen.txt")); while((r.readLine()) != null){ lft = r.readLine(); loon = r.readLine(); System.out.println(Integer.parseInt(lft)); System.out.println(Integer.parseInt(loon)); //setLoon(Integer.parseInt(lft),Integer.parseInt(r.readLine())); } //System.out.println(leeftijden[0].toString()); }
as you can see the 3th & the 4th line where swapped and 18 and 2400 aren't printed.18
1200
1600
18
18
1100
@ cselic: tried your code but the result is the same.
greets
- 05-27-2010, 05:50 PM #6
Change:
toSystem.out.println(Integer.parseInt(lft));
System.out.println(Integer.parseInt(loon));
System.out.println("lft=" + lft + "<");
System.out.println("loon=" + loon + "<");
To show the String input before trying to convert it.
- 05-27-2010, 07:37 PM #7
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Hello,
Strings are red fine now. I didn't count the readline-command in the while statement.
The output is now:
But now, when i enter this lineslft=18<
loon=1200<
lft=18<
loon=1600<
lft=18<
loon=2400<
lft=18<
loon=1100<
So I just convert the strings and don't use them at all.Java Code:Integer.parseInt(lft); Integer.parseInt(loon);
The next output and error occurs:
It seems that the error only occurs by 'lft' and not by 'loon' (I checked that by setting one line as a command and only convert the other parameter)lft=18<
loon=1200<
java.lang.NumberFormatException: For input string: "18
- 05-27-2010, 07:43 PM #8
Where does the leading " come from?For input string: "18
What line of code does the error occur on?
- 05-27-2010, 07:50 PM #9
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
There's also " after the number. Bad copy-paste from me.
The error points to "Integer.parseInt(lft);"
- 05-27-2010, 08:42 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-27-2010, 08:50 PM #11
Don't understand what is happening.
You say that lft = "18"
Then Integer.parseInt(lft); should work.
The code you posted doesn't make sense. Usually when converting a String to an int, you assign the value or display it. Your posted code shows neither:
The error points to "Integer.parseInt(lft);"
When posting code or error messages don't edit them, copy and paste them completely.
So far by not showing the full text you've caused us to ask more questions. We need to see it all.
Could you show more code for where the error occurs. Show at least 5 lines up to the line where the error occurs.
- 05-27-2010, 09:08 PM #12
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Here's the full code:
and the full error message:import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
public class Enquete{
private
Hashtable<Integer,Integer> leeftijden[];
String lft,loon;
public Enquete() throws NumberFormatException, IOException {
leeftijden = new Hashtable[65-18];
// lonen inlezen
BufferedReader r = new BufferedReader(new FileReader("lonen.txt"));
while((lft = r.readLine()) != null){
loon = r.readLine();
System.out.println("lft=" + lft + "<");
System.out.println("loon=" + loon + "<");
Integer.parseInt(lft);
//Integer.parseInt(loon);
//setLoon(Integer.parseInt(lft),Integer.parseInt(r.r eadLine()));
}
//System.out.println(leeftijden[0].toString());
}
private void setLoon(Integer leeftijd, Integer loon2) {
if(leeftijden[leeftijd-18] == null)
leeftijden[leeftijd-18].put(loon2, 1);
else if(!leeftijden[leeftijd-18].contains(loon2))
leeftijden[leeftijd-18].put(loon2, 1);
else{
int temp = leeftijden[leeftijd-18].get(loon2);
leeftijden[leeftijd-18].put(loon2, temp++);
}
}
public static void main(String Args []){
try {
new Enquete();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Remark the symbols before '18' again. The only difference now is that I opened it with Eclipse for Windows and the rest of the day I was working in Ubuntu, so Eclipse for Linux. But could this affect the result? The code is still the same...Java Code:lft=18< loon=1200< java.lang.NumberFormatException: For input string: "18" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Enquete.<init>(Enquete.java:21) at Enquete.main(Enquete.java:42)
Hope you can help me further now.
As extra information. this is the exercise:So I was thinking about an array of Hashtables with the salary as a key.Input: a textfile with the ages and salaries of some people. The age is between 18 and 65 years.
Output: The salaries sorted by age and how many a person of a specific age has this salary.Last edited by genji; 05-27-2010 at 09:23 PM.
- 05-27-2010, 09:33 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Sum of Integers - From File
By Cubba27 in forum New To JavaReplies: 8Last Post: 03-25-2010, 11:45 AM -
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01-28-2010, 10:29 AM -
Integers and Lists
By TGH in forum New To JavaReplies: 8Last Post: 01-27-2010, 09:49 AM -
how to get the Integers out of a String
By JordashTalon in forum New To JavaReplies: 10Last Post: 01-30-2009, 06:28 PM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks