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?