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 6 of 6

Thread: Reading from a file, multiple lines

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Reading from a file, multiple lines

    This isn't homework, I'm only in 8th grade my school doesn't have java courses before you ask)<--
    Well I ran into a funny little problem, not quite sure how I should approach it;
    Well I have a file looking like this:
    1 0 3 2
    2 3 4 5

    And so on going down like 20 lines.
    Here is my code:
    It is meant to read the file, and to test it I had it print out the lines, but it only prints out the first line of the file.
    I know that my method readLine(); reads the first line only, how would I get it to read the next line?
    public class getNumbersFromFile
    {
    	public static void main(String[] args) {
    		getNumbersFromFile file = new getNumbersFromFile();
     	file.getFileFromMethod();
     
    	}
    	public void getFileFromMethod(){
     
    try {
     
     
    			BufferedReader FileReader = new BufferedReader(new FileReader("SudokuBoard.txt"));
    			String Text = FileReader.readLine();
     
    			String[] Split = Text.split(" ");
    			System.out.println(Text);
     
     
    			} catch (IOException e) {
    				System.out.println("Some sort of interruption.");
    				System.out.println(e);
    			}
     
    		}
    }


  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Reading from a file, multiple lines

    if u want to display all lines of txt change this
    String[] Split = Text.split(" ");
    System.out.println(Text);

    to this
    String[] Split = Text.split(" ");
    int count = 0
    while ( Split[count] != null ) {
    System.out.println(Split[count]);
    count = count + 1
    }
    that should work i think
    but the main problem from what i got was that u were displaying text, not the Split array
    Programming: the art that fights back

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading from a file, multiple lines

    Quote Originally Posted by wolfgar View Post
    if u want to display all lines of txt change this
    String[] Split = Text.split(" ");
    System.out.println(Text);

    to this
    String[] Split = Text.split(" ");
    int count = 0
    while ( Split[count] != null ) {
    System.out.println(Split[count]);
    count = count + 1
    }
    that should work i think
    but the main problem from what i got was that u were displaying text, not the Split array
    I did that;
    What i get as output,then error:
    0
    0
    0
    0
    6
    0
    0
    0
    9
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    at getNumbersFromFile.getFileFromMethod(getNumbersFro mFile.java:25)
    at getNumbersFromFile.main(getNumbersFromFile.java:10 )

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Reading from a file, multiple lines

    Here is a snippet of code to read a file, line by line.

        public static void main(String[] args) throws Exception {
            final BufferedReader bufferedReader = new BufferedReader(new FileReader("SudokuBoard.txt"));
     
            if (bufferedReader != null) {
                String line;
     
               // Keep on looping while line is not null, readLine will return null when it reaches the end of the file and the loop will stop.
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
     
                bufferedReader.close();
            }
     
        }

    See also
    http://www.javaprogrammingforums.com...line-file.html

    // Json

  5. #5
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Reading from a file, multiple lines

    Quote Originally Posted by Json View Post
    Here is a snippet of code to read a file, line by line.

        public static void main(String[] args) throws Exception {
            final BufferedReader bufferedReader = new BufferedReader(new FileReader("SudokuBoard.txt"));
     
            if (bufferedReader != null) {
                String line;
     
               // Keep on looping while line is not null, readLine will return null when it reaches the end of the file and the loop will stop.
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
     
                bufferedReader.close();
            }
     
        }

    See also
    http://www.javaprogrammingforums.com...line-file.html

    // Json
    Jason I love you .
    It works, and I'm reading the post you linked.

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Reading from a file, multiple lines

    Glad I could help

    // Json

Similar Threads

  1. [SOLVED] File Reading from the specified location
    By rajesh.mv in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 13th, 2009, 12:56 AM
  2. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM
  3. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 03:51 AM
  4. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM
  5. How to assign values from a file to an arraylist of objects?
    By nadman123 in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2008, 01:07 PM