Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: FileReader need assistance

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default FileReader need assistance

    hi folks.

    uhm how do i read all the inputs from a file, well the problem is it only reads the first line? can you help me, im not yet familiar with the methods thanks
    Here's the main class
    import java.util.*;
    import java.io.*;
    public class readFile
    {
    	public static void main(String[]args) throws IOException
    	{
    		Scanner sc = new Scanner(new FileReader("cool.in"));
     
    		int min = Integer.parseInt(sc.next());
    		int max = Integer.parseInt(sc.next());
     
    		System.out.println(min+" "+max);
     
    	}
    }

    and here's the "cool.in" file to be read
    1 10
    20 30
    40 50
    60 70


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: FileReader need assistance

    the scanner.next() method reads in a "word" (aka, stuff separated by whitespaces).

    So, the first call to next() would return "1", the second one would return "10", the third "20", and so on until there's nothing left to read.

    What you can do if you want to read in all the inputs is use a while loop to read in all the values. This will require some data structure to hold all the values (usually a list of some sort), unless you don't want to save the values you read in.

    Scanner reader = new Scanner(new FileReader("cool.in"));
     
    while (reader.hasNext())
    {
         System.out.println("Reading min... " + reader.next());
         System.out.println("Reading max..." + reader.next());
    }

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileReader need assistance

    thanks for the quick reply..im adding some info if you dont mind

    well im gonna also used the first column as "min" and the next column for "max"
    its like this
    for(int x=min ;x <=max; x++)
    {
    [I]script[/I]
    }

    something like that
    the problem is it only reads the first line which is
    1 10
    Last edited by tazjaime; November 7th, 2009 at 08:47 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: FileReader need assistance

    Just put it inside the the while loop:

    while (reader.hasNext())
    {
         int min = Integer.parseInt(reader.next());
         int max = Integer.parseInt(reader.next());
         for(int x=min ;x <=max; x++)
         {
              [i]script[/i]
         }
    }

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FileReader need assistance

    thank you very much i should include the min and max for it to be included thank you problem sovled