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

Thread: Practicing reading from file.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Practicing reading from file.

    Hello, I am practicing reading from a text-file into arrays. I have encountered a problem I am not able to solve.
    I have a file called "nameage.txt":
    Tom        44
    Arnold     98
    Susie      24
    Johnny     17

    My goal is to read this info into two different arrays, a string array called name, and an integerer array called age. Here is my attempt:
    import java.util.Scanner;
    import java.io.File;
     
    class nameAge {
        public static void main(String[] args) {
    	String[] name = new String[4];
    	int[] age = new int[4];
    	try {
    	    Scanner reader = new Scanner(new File("nameage.txt"));
    	    String[] line = new String[2];
    	    int i = 0;
    	    while (reader.hasNextLine()) {
    		line = reader.nextLine().split(" ");
    		name[i] = line[0];
    		//age[i] = Integer.parseInt(line[1]);
    		i++;
    	    }
    	}
    	catch(Exception e) {
    	    System.out.println("Reading didn't work.");
    	}
     
        }
    }
    Please note the part I have commented out, it is the reading of the ages. If I don't have that part with me, the reading of the names goes perfectly fine and I have checked that the array is correct. But if I try to uncomment the one line and read in the ages aswell the program will compile but an exception is caught, can you guys see what is wrong with the code?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Practicing reading from file.

    You need to see what the exception is. Change the catch block to call the printStackTrace() method which will print out the text of the error message. Given that you can work on the problem.

    Also try debugging the code by adding some println statements.
    Print out the contents of the line that is read from the file and the contents of the array created by split.
    The Arrays class's toString() method will format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Practicing reading from file.

    [Tom, , , , , , , , 44]
    [Arnold, , , , , 98]
    [Susie, , , , , , 24]
    [Johnny, , , , , 17]

    Thanks I used your last codebit, and this is what i got. The problem is in the split it seems. What I want is that the split is to delete all the spaces and create and array where the element is what is between the spaces.

    From what I read it seems that split("\\s") should do it, but it still does not work, hm.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Practicing reading from file.

    What is printed out when the contents of the line that is read (returned by nextLine) was printed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Practicing reading from file.

    Quote Originally Posted by Norm View Post
    What is printed out when the contents of the line that is read (returned by nextLine) was printed?
    I added this code in the while loop: System.out.println(Arrays.toString(line));
    And the output became:
    [Tom, , , , , , , , 44]
    [Arnold, , , , , 98]
    [Susie, , , , , , 24]
    [Johnny, , , , , 17]
    It is one array for each pass in the while-loop. It seems that I split the string from the text-file, but the spaces are still there.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Practicing reading from file.

    What is printed out when the contents of the line that is read (returned by nextLine) was printed?
    You posted the output from the Arrays class's toString() method.


    I guess your problem is with what regex to use to skip any number of spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Practicing reading from file.

    Quote Originally Posted by Norm View Post
    What is printed out when the contents of the line that is read (returned by nextLine) was printed?
    You posted the output from the Arrays class's toString() method.


    I guess your problem is with what regex to use to skip any number of spaces.
    Ok, sorry I misunderstood what you wanted. I changed the code so that in each pass in the while loop it also prints out each of the content in the line array:
    import java.util.*;
    import java.io.File;
     
     
    class nameAge {
        public static void main(String[] args) {
    	String[] name = new String[4];
    	int[] age = new int[4];
    	try {
    	    Scanner reader = new Scanner(new File("nameage.txt"));
    	    String[] line = new String[2];
    	    int i = 0;
    	    while (reader.hasNextLine()) {
    		line = reader.nextLine().split(" ");
    		for (String s: line) {////////Here I print out the content.
    		    System.out.println(s);////
    		}//////////////////////7//////
    	    }
    	}
    	catch(Exception e) {
    	    e.printStackTrace() ;
    	}
     
        }
    }
    The output in the terminal is:
    Tom
     
     
     
     
     
     
     
    44
    Arnold
     
     
     
     
    98
    Susie
     
     
     
     
     
    24
    Johnny
     
     
     
     
    17

    It still seems that what I wrote earlier happens. The spaces does not disappear.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Practicing reading from file.

    That's not what I was asking. I asked for the line that was read to be printed. Something like this:
       String theLine = reader.nextLine();       //  Read the line
       System.out.println("theLine="+theLine);  //  print the line

    You need to research how to write a regex to skip one or more spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    jjk (January 1st, 2013)

  10. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Practicing reading from file.

    Quote Originally Posted by Norm View Post
    That's not what I was asking. I asked for the line that was read to be printed. Something like this:
       String theLine = reader.nextLine();       //  Read the line
       System.out.println("theLine="+theLine);  //  print the line

    You need to research how to write a regex to skip one or more spaces.
    Yeah, you are correct. I thought it would automatically would be done with split(" ") or split("\\s"), but I need to use split("\\s+"). Then the code works.

    Thank you veru much for your time!

Similar Threads

  1. [SOLVED] Help with reading from file
    By Dr.Code in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: July 2nd, 2012, 07:44 AM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  4. Reading from a file
    By NeedzABetterSN in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2011, 08:07 AM
  5. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 03:14 AM