Results 1 to 13 of 13
  1. #1
    genji is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default read integers from txt

    Hi,

    I have a txt file like this

    Java Code:
    18
    1200
    18
    1600
    18
    2400
    18
    1100
    I have to read the Integers to use them further in the program, but I always get an error like this

    Exception in thread "main" java.lang.NumberFormatException: For input string: "18"
    I don't understand where the symbols come from nor how to ignore them.
    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 ;-)

    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());
    	}
    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.

    Thanks,
    Genji

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    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.
    setLoon(Integer.parseInt(s),Integer.parseInt(r.rea dLine())
    Use println() to display values before trying to convert them to ints.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Might be unicode or some other format..check the file format.
    Last edited by RamyaSivakanth; 05-27-2010 at 04:34 PM.
    Ramya:cool:

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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();
    			}
    		}
    	}
    }

  5. #5
    genji is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Hi

    I don't understand why this can be a unicode problem.
    When I change the file to

    18
    1200
    18
    1600
    18
    2400
    18
    1100
    (inserting a white line at the beginning)

    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:
    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());
    	}
    This gives the following result:
    18
    1200
    1600
    18
    18
    1100
    as you can see the 3th & the 4th line where swapped and 18 and 2400 aren't printed.


    @ cselic: tried your code but the result is the same.

    greets

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Change:
    System.out.println(Integer.parseInt(lft));
    System.out.println(Integer.parseInt(loon));
    to
    System.out.println("lft=" + lft + "<");
    System.out.println("loon=" + loon + "<");

    To show the String input before trying to convert it.

  7. #7
    genji is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Hello,

    Strings are red fine now. I didn't count the readline-command in the while statement.
    The output is now:
    lft=18<
    loon=1200<
    lft=18<
    loon=1600<
    lft=18<
    loon=2400<
    lft=18<
    loon=1100<
    But now, when i enter this lines
    Java Code:
    Integer.parseInt(lft);
    Integer.parseInt(loon);
    So I just convert the strings and don't use them at all.

    The next output and error occurs:
    lft=18<
    loon=1200<
    java.lang.NumberFormatException: For input string: "18
    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)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    For input string: "18
    Where does the leading " come from?

    What line of code does the error occur on?

  9. #9
    genji is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    There's also " after the number. Bad copy-paste from me.

    The error points to "Integer.parseInt(lft);"

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by genji View Post
    There's also " after the number. Bad copy-paste from me.

    The error points to "Integer.parseInt(lft);"
    What is the size of your original file and on what operating system (i.e. MS Windows or some sort of Unix etc)?

    kind regards,

    Jos

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    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.

  12. #12
    genji is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Here's the full code:

    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();
    }
    }

    }
    and the full error message:
    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)
    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...

    Hope you can help me further now.
    As extra information. this is the exercise:
    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.
    So I was thinking about an array of Hashtables with the salary as a key.
    Last edited by genji; 05-27-2010 at 09:23 PM.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    You're working on Linux; can you od the data file and show its dump here? (Read "man od" if you're not familiar with od).

    kind regards,

    Jos

Similar Threads

  1. Sum of Integers - From File
    By Cubba27 in forum New To Java
    Replies: 8
    Last Post: 03-25-2010, 11:45 AM
  2. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM
  3. Integers and Lists
    By TGH in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 09:49 AM
  4. how to get the Integers out of a String
    By JordashTalon in forum New To Java
    Replies: 10
    Last Post: 01-30-2009, 06:28 PM
  5. Replies: 5
    Last Post: 10-17-2008, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •