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: Text File into String Array

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

    Default Text File into String Array

    I'm a complete beginner to Java Programming. I'm having difficulties trying to crack the following piece of code.

    What I want the code to do is get the values from a text file which just has 1 number on each line like so :

    20
    10
    30


    The problem with the code I made is it is not storing each number in a separate index (eg. results[0] = 20, results [1]= 10 and so on..) . So when I execute the following code "System.out.println( result[0]);", it shows me all the numbers in the file instead of just the first number (eg. 20).


         /******* Put Text File into Array************/
     
          try 
          {
             FileReader in = new FileReader ("highscores.txt");
             BufferedReader br = new BufferedReader(new BufferedReader(in));
     
             String line;
     
    	 while ((line = br.readLine()) !=null)
    	 {
     
    	    String[] result = line.split("\r\n"); 
     
    	    for (int x=0; x<result.length; x++)
    	    {
    	       System.out.println( result[x]); 
    	    }
             }	
     
          }//try
     
          catch (Exception e)
          {
             System.err.println("Error: " + e.getMessage()); //Catch exception if any and show error message
          }
    Can anyone help me with a solution?
    Last edited by mathanv; December 11th, 2010 at 01:40 PM.


  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: Text File into String Array

    	 while ((line = br.readLine()) !=null)
    	 {
     
    	    String[] result = line.split("\r\n"); 
     
    	    for (int x=0; x<result.length; x++)
    	    {
    	       System.out.println( result[x]); 
    	    }
             }
    2 things:
    1. there's no reason to call line.split(), this is done automatically by calling readLine().
    2. You're printing out the entire contents of the array in the for loop. Instead, print out only the item you put into the array. You'll need an external counter (one outside the while loop) to keep track of where you're adding the next line of text into.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text File into String Array

    2 things:
    1. there's no reason to call line.split(), this is done automatically by calling readLine().
    2. You're printing out the entire contents of the array in the for loop. Instead, print out only the item you put into the array. You'll need an external counter (one outside the while loop) to keep track of where you're adding the next line of text into.
    Yh, I amended the code below. In the original code I wanted to print out the contents of the whole array but it was storing all the numbers in the index "0". In the code below, I was advised to use an ArrayList since I don't know the definite size of the array.

    Still having one little problem with the amended code though, when I compile the following code:

    try 
          {
             FileReader in = new FileReader ("highscores.txt");
             BufferedReader br = new BufferedReader(new BufferedReader(in));
     
             String line;
     
    	 ArrayList<String> results = new ArrayList<String>(); //declaring my arrya list
     
    	 while ((line = br.readLine()) !=null)
    	 {
    	    results.add(line); //add to array
             }
     
    	System.out.println(results[0]);
     
          }//try
     
          catch (Exception e)
          {
             System.err.println("Error: " + e.getMessage()); //Catch exception if any and show error message
          }

    I get the compiler error message : guess1.java:237: array required, but java.util.ArrayList<java.lang.String> found
    System.out.println(results[0]);


    Then I just changed: System.out.println(results[0]); into System.out.println(results);
    and got the following in the terminal : [20, 10, 20, 20, 20]

    How would I just print out the first number in the arrayList? I'm guessing it's not the same as Arrays where the first number would be "results[0]"?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,544
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Text File into String Array

    An array is different than an ArrayList. The main difference is that an ArrayList is an object, while an array is not.

    Since array is not an object, you cannot use methods to interact with its values. Instead, you use brackets to reference its values. For example, you would use this call:
    array[0];
    to access the 0 index in the array.

    On the flip side, since ArrayList is an object, you use methods to interact with its values. These methods can be found in the ArrayList API(ArrayList (Java Platform SE 6)). Specifically, the method to get a value at a given index in the ArrayList class is the get(int index) method. So, to access the value at index 0 in an ArrayList, you would call:
    arraylist.get(0);

    So, your call to:
    System.out.println(results[0]);
    Is wrong because results is an ArrayList. To get the value of index 0 in results, you have to use a method. That method is the get method, so you would say:
    System.out.println(results.get(0));

    I hope that information helps, it is very important to learn the difference between arrays and object data structures.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text File into String Array

    Thanks for clearing everything up mate, well appreciated!

Similar Threads

  1. Reading in entire contents of text file to one string
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: December 12th, 2010, 06:03 PM
  2. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 04:31 PM
  3. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 10:56 AM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM