Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Text File into String Array

823102Dec 11 2010 — edited Dec 16 2010
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
      }
{code}
Can anyone help me with a solution?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2011
Added on Dec 11 2010
18 comments
164 views