Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default how to read two numbers at a time as input from a text file in java

    Hi guys, I just need to know how to take two numbers at a time as input from text file as i have to assign these two numbers as i,j so that i can compare them with other values
    my input file contains two numbers per line separated with " " i.e,
    1 2
    2 3
    4 5.............

    i nearly have 1000 pairs as my input
    i hope you people reply as soon as possible because i am stuck with this and with out completing this part of code i cannot go further please

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

    Default Re: how to read two numbers at a time as input from a text file in java

    There are several options:

    1) take a look at the API documentation for the Scanner class;
    2) read a line, String.split( ... ) it and Integer.parseInt( ... ) the elements.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: how to read two numbers at a time as input from a text file in java

    Hey do you mean like this:
    FileInputStream file = new FileInputStream("E:/clusters.txt");
    Scanner inputFile = new Scanner(file);
    String line = inputFile.nextLine( );
    final String[] array = line.split(" ");
    But my question how can i assign i,j values
    int i = Integer.parseInt( line ) but what about j value

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

    Default Re: how to read two numbers at a time as input from a text file in java

    Quote Originally Posted by priyankakusuma View Post
    Hey do you mean like this:
    FileInputStream file = new FileInputStream("E:/clusters.txt");
    Scanner inputFile = new Scanner(file);
    String line = inputFile.nextLine( );
    final String[] array = line.split(" ");
    But my question how can i assign i,j values
    int i = Integer.parseInt( line ) but what about j value
    Do this instead:

    Java Code:
    int i = Integer.parseInt( array[0] );
    int j = Integer.parseInt( array[1] );
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: how to read two numbers at a time as input from a text file in java

    Quote Originally Posted by JosAH View Post
    Do this instead:

    Java Code:
    int i = Integer.parseInt( array[0] );
    int j = Integer.parseInt( array[1] );
    kind regards,

    Jos
    Hey as you have suggested I have tried and i am getting error as
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:573)
    at java.lang.Integer.parseInt(Integer.java:596)
    at sample.main(sample.java:29)
    where i have gone wrong .This is my code:

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class sample{
        public static void main(String args[]) throws FileNotFoundException
        {
            FileInputStream file = new FileInputStream("E:/clusters.txt"); 
    Scanner inputFile = new Scanner(file); 
    String line = inputFile.nextLine( );
    final String[] array = line.split(" ");
    int i = Integer.parseInt( array[0] );
    int j = Integer.parseInt( array[1] );
    System.out.println("i="+i+",j="+j);
    }
    }
    my input is in the text file clusters.txt:
    1 2
    2 3
    4 5
    11 2
    1 5.......... and i nearly have 1000 pairs in my file
    and my expected output is:
    i=1,j=2
    i=3,j=4.....
    I hope you could help me
    Last edited by pbrockway2; 04-01-2013 at 03:56 AM. Reason: code tags added

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to read two numbers at a time as input from a text file in java

    I've added code tags to your post. To make the post readable put [code] at the start of the code and [/code] at the end. It will also help if you indent the contents of {}-blocks.

    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:573)
    at java.lang.Integer.parseInt(Integer.java:596)
    at sample.main(sample.java:29)
    A NumberFormatException means that you are trying to parse a string into an integer, but the string doesn't make sense as an integer. The message is telling you that this happened on line 29 of sample.java. So, which is line 29?

    A good technique is to print the strings before you parse them. It might be a good idea to print the line you read as well. Something like:

    Java Code:
    public static void main(String args[]) throws FileNotFoundException
    {
        FileInputStream file = new FileInputStream("E:/clusters.txt"); 
        Scanner inputFile = new Scanner(file); 
        String line = inputFile.nextLine( );
        System.out.println("Read line -->" + line + "<--");
    
        final String[] array = line.split(" ");
        System.out.println("array has length " + array.length);
        System.out.println("array[0] is -->" + array[0] + "<--");
        System.out.println("array[1] is -->" + array[1] + "<--");
    
        int i = Integer.parseInt( array[0] );
        int j = Integer.parseInt( array[1] );
        System.out.println("i="+i+",j="+j);
    }
    I've used arrows around the strings because spaces are a good source of errors when you are parsing ints as strings.

  7. #7
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: how to read two numbers at a time as input from a text file in java

    Quote Originally Posted by pbrockway2 View Post
    I've added code tags to your post. To make the post readable put [code] at the start of the code and [/code] at the end. It will also help if you indent the contents of {}-blocks.



    A NumberFormatException means that you are trying to parse a string into an integer, but the string doesn't make sense as an integer. The message is telling you that this happened on line 29 of sample.java. So, which is line 29?

    A good technique is to print the strings before you parse them. It might be a good idea to print the line you read as well. Something like:

    Java Code:
    public static void main(String args[]) throws FileNotFoundException
    {
        FileInputStream file = new FileInputStream("E:/clusters.txt"); 
        Scanner inputFile = new Scanner(file); 
        String line = inputFile.nextLine( );
        System.out.println("Read line -->" + line + "<--");
    
        final String[] array = line.split(" ");
        System.out.println("array has length " + array.length);
        System.out.println("array[0] is -->" + array[0] + "<--");
        System.out.println("array[1] is -->" + array[1] + "<--");
    
        int i = Integer.parseInt( array[0] );
        int j = Integer.parseInt( array[1] );
        System.out.println("i="+i+",j="+j);
    }
    I've used arrows around the strings because spaces are a good source of errors when you are parsing ints as strings.
    Hey I have tried the code you have given but here i am getting the output from 1st line only i.e,I am getting only 1 pair as output but as i said earlier my input file contains nearly 1000 pairslike:
    1 2
    3 4
    2 5..........

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

    Default Re: how to read two numbers at a time as input from a text file in java

    Quote Originally Posted by priyankakusuma View Post
    Hey I have tried the code you have given but here i am getting the output from 1st line only i.e,I am getting only 1 pair as output but as i said earlier my input file contains nearly 1000 pairslike:
    1 2
    3 4
    2 5..........
    That is because your code reads only one line; it can't do miracles ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: how to read two numbers at a time as input from a text file in java

    Hey I am able to print all the pairs but while running I am getting some errors like these:
    run:

    i=31,j=79
    i=31,j=90
    i=31,j=91
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    i=31,j=98

    at java.lang.NumberFormatException.forInputString(i=3 8,j=87
    i=38,j=91
    i=38,j=98
    i=39,j=45
    NumberFormatException.java:65i=39,j=46
    )

    i=40,j=44
    i=40,j=52
    at java.lang.Integer.parseInt(i=40,j=65
    Integer.java:573)
    i=40,j=73
    i=40,j=76
    i=40,j=81
    i=41,j=42
    i=41,j=46
    at java.lang.Integer.parseInt(i=41,j=47
    Integer.java:596)
    i=41,j=50
    i=41,j=53
    i=41,j=54
    at sample.main(sample.java:17)
    i=41,j=56

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

    Default Re: how to read two numbers at a time as input from a text file in java

    That exception indicates that there are some lines in your file that don't have the correct format; check it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Default Re: how to read two numbers at a time as input from a text file in java

    Quote Originally Posted by JosAH View Post
    That exception indicates that there are some lines in your file that don't have the correct format; check it.

    kind regards,

    Jos
    Thank you so much Jos ,Can you just check my other post:
    how to form clusters of number pairs using java

Similar Threads

  1. java read n write to file with unicode text
    By kcananda in forum New To Java
    Replies: 1
    Last Post: 08-13-2012, 05:50 PM
  2. Replies: 1
    Last Post: 09-30-2010, 02:36 AM
  3. read only double input from text file
    By napi1234 in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 04:06 PM
  4. How can we read text file in java 1.4
    By javasaravanan in forum Advanced Java
    Replies: 4
    Last Post: 11-30-2009, 03:28 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Tags for this Thread

Posting Permissions

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