4

I'm working on a review assignment for school. The assignment is to write a class that reads from standard input a file containing several integers, which are to be put into an array. From here, methods need to be written that find the average, median, max, min, and standard deviation.

It reads like so:
45
56
67
78
89 etc...

So, I'm assuming I need to create an array list (since the length is undefined) and use scanner to read each line for an integer, then create the methods that will pick apart what I need. However, I fail to understand how to properly use FileReader and Scanner in conjunction. I'm currently running BlueJ. The text file is located under the project folder, yet the file is never found by the code.

Here is what I have so far.

import java.io.*; import java.util.*; import java.math.*; public class DescriptiveStats { public DescriptiveStats(){} public FileReader file = new FileReader("students.txt"); public static void main(String[] args) throws IOException { try{ List<Integer> scores = new ArrayList<Integer>(); Scanner sc = new Scanner(file); while(sc.hasNext()) { scores.add(sc.nextInt()); } sc.close(); } catch(Exception e) { e.printStackTrace(); } } 
2
  • Your code works fine for me. Do you mean that you have an FileNotFoundException, or you get another problem on accessing the values in your list "scores"? Commented Sep 6, 2011 at 3:02
  • I have a FileNotFoundException, so I can't really see the output to determine whether or not what I'm doing is correct. I'm using blueJ, and the file is located in the same directory that the .java files for the project are. Commented Sep 6, 2011 at 3:25

3 Answers 3

2

Make sure "students.txt" is located in the same directory as the code you are running(eg whereever your .java files get compiled to), or put the full path to the file..ie("C:/folder/students.txt")

Sign up to request clarification or add additional context in comments.

4 Comments

I've actually tried both in this instance - Scanner sc = new Scanner(new FileReader("C:/Users/xxx/students.txt")); is what it is currently. Still no go. Is there a better IDE for doing this? Also, after compiling, I right click on the class and hit "void main(String[] args)" then I'm brought to a window that reads "DescriptiveStats.main(blank field with {})" then hit okay. then I get a FileNotFoundException
I have just tried on BlueJ and I think it works here too. There may be another compiling problem.
Thank you Yasin - so you were able to read from a file? I thought I may just be missing something minute. I'm downloading Eclipse right now.
So you got the solution? Or still have problem?
1

System.out.println(new File("students.txt").getAbsolutePath()); will give you the path from where java is trying to load the file.

I suspect its due to ambiguity caused by multiple paths in classpath, the first entry being the one from where it loads. Setting the file load path as the first entry should solve the problem.

Comments

0

Use Scanner.hasNextInt() (instead of Scanner.hasNext()):

... while(sc.hasNextInt()) { scores.add(sc.nextInt()); } ... 

1 Comment

Actually it works with hasNext(), I don't think it is the reason.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.