Java: Scanner from File into Arraylist, then min/max/mean/standarddeviation
The assignment is to create a program that will read from standard input (a file containing a list of integers) into an array, and then finding the mean, max, min, median, and standard deviation of those integers. first things first, here's the code:
import java.io.*;
import java.util.*;
public class DescriptiveStats
{
protected List<Integer> scores = new ArrayList<Integer>();
public DescriptiveStats()
{
// default constructor
}
public void main(String[] args) throws IOException
{
try{
Scanner sc = new Scanner(new FileReader("students.txt"));
while(sc.hasNextInt())
{
scores.add(sc.nextInt());
}
sc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
DescriptiveStats stat = new DescriptiveStats();
System.out.println("Min = " + stat.getMin(scores[]));
System.out.println("Max = " + stat.getMax(scores[]));
System.out.println("Median = " + stat.getMedian(scores[]));
System.out.println("Mean = " + stat.getMean(scores[]));
int Mean = stat.getMean(scores[]);
System.out.println("Standard Deviation = " + stat.getStandardDev(scores[], Mean));
}
public int getMin(int []lst)
{
int min = lst[0];
for(int i=0;i<lst.length;i++)
{
if(min>lst[i])
min=lst[i];
}
return min;
}
public int getMax(int []lst)
{
int max = lst[0];
for(int i=0;i<lst.length;i++)
{
if(max<lst[i])
max=lst[i];
}
return max;
}
public int getMedian(int [] lst)
{
Arrays.sort(lst);
int middle = lst.length/2;
if(lst.length%2==1)
{
return lst[middle];
}
return (lst[middle-1] + lst[middle]);
}
public int getMean(int [] lst)
{
Arrays.sort(lst);
int mean = 0;
int sum = 0;
int count = 0;
for(int i=0;i>lst.length;i++)
{
sum = sum + lst[i];
count++;
}
mean = sum/count;
return mean;
}
public int getStandardDev(int [] lst, int m)
{
int mean = m;
int [] array = lst;
int total = 0;
for(int i =0; i < array.length; i++)
{
int result = array[i]-mean;
int [] all = Math.pow(result, 2);
}
total = result/array.length;
int standev = Math.sqrt(total);
return standev;
}
}
My particular issues come down to this: I don't know if I'm reading in the file correctly. I've followed many instructions from books to website and that seems to be the correct formula for scanner, but the file isn't found. I've tried three different IDE's (BlueJ, Eclipse, Netbeans) to no avail.
Secondly, I've created the methods to take an arraylist as input and called them in a 'public void main(string[] args)' method. I'm pretty sure i can do this, but it won't compile, and i'm not sure what i'm missing开发者_如何学JAVA.
Finally, I want to make sure that my standard deviation method is correct. I don't expect any one person to solve all of these, but those are the points at which I'm stuck, and any help would be appreciated. Thank you!
The first thing to do is to get your program to compile and run (correctly or not). Then you can simply print the values it reads and test whether they are identical to the values in the data file.
To get your program to run, you first need to declare the main() method to be static
. Also, since you're catching the IOException (good idea, by the way!), you don't need to declare main
to throw the exception. Finally, since you are trying to read the data inside main
, you can't access the scores
field, since it's an instance variable and you don't have an instance of DescriptiveStats
yet. Besides, since you're passing the array as a parameter to the analysis functions, it doesn't need to be an instance variable. Try this instead:
public static void main(String[] args) {
List<Integer> scores = new ArrayList<Integer>();
try{
Scanner sc = new Scanner(new FileReader("students.txt"));
while(sc.hasNextInt())
{
scores.add(sc.nextInt());
}
sc.close();
}
catch(Exception e)
{
e.printStackTrace();
}
// etc.
}
If you want to know from where the program tries to read the file, use this:
File students = new File("students.txt");
System.out.println("Reading data from :" + students.getAbsolutePath());
Scanner sc = new Scanner(new FileReader(students));
...
Where do you have placed the file "students'txt". If it is not in the same location as the class "DescriptiveStats", JVM won't be able to locate the file. So, make sure that the file is in the same path as the class or specify the correct path if you want to take it from somewhere else.
Coming to the second point. You can absolutely call these methods from the main() function. What is the compiler error you are getting?
Appreciate your effort in trying out with different IDEs. But it has nothing got to do with the IDE.
Okay, so I made some changes in accordance to what people were suggesting. I did away with the hard coded file, and replaced it with standard input. Now, my real focus is: -Making sure that I'm using array-lists in conjunction with the methods correctly, -Making sure that I'm correctly able to apply those to standard deviation Here is my new code:
import java.io.*;
import java.util.*;
/**
* Read from a standard input a series of exam scores,
* then compute the min, max, avg, med, & stand deviation.
*/
public class DescriptiveStats
{
public DescriptiveStats()
{
// default constructor
}
public static void main(String[] args) throws IOException
{
ArrayList<Integer> scores = new ArrayList<Integer>();
try{
Scanner input = new Scanner(System.in);
while(input.hasNextInt())
{
scores.add(input.nextInt());
}
input.close();
}
catch(Exception e)
{
e.printStackTrace();
}
DescriptiveStats stat = new DescriptiveStats();
System.out.println("Min = " + stat.getMin(scores));
System.out.println("Max = " + stat.getMax(scores));
System.out.println("Median = " + stat.getMedian(scores));
System.out.println("Mean = " + stat.getMean(scores));
int Mean = stat.getMean(scores);
//System.out.println("Standard Deviation = " + stat.getStandardDev(scores, Mean));
}
public int getMin(ArrayList<Integer> lst)
{
int min = 0;
int d = 0;
for(int i=0;i<lst.size();i++)
{
d = lst.get(i);
if(min>d)
{
min=d;
}
}
return min;
}
public int getMax(ArrayList<Integer> lst)
{
int max = lst.get(0);
for(int i=0;i<lst.size();i++)
{
if(max<lst.get(i))
max=lst.get(i);
}
return max;
}
public int getMedian(ArrayList<Integer> lst)
{
Collections.sort(lst);
int middle = lst.size()/2;
if(lst.size()%2==1)
{
return lst.get(middle);
}
return (lst.get(middle-1) + lst.get(middle));
}
public int getMean(ArrayList<Integer> lst)
{
Collections.sort(lst);
int mean = 0;
int sum = 0;
int count = 0;
for(int i=0;i>lst.size();i++)
{
sum = sum + lst.get(i);
count++;
}
mean = sum/count;
return mean;
}
/**
public int getStandardDev(ArrayList lst, int m)
{
int mean = m;
int [] array = lst;
int total = 0;
for(int i =0; i < array.length; i++)
{
int result = array[i]-mean;
int [] all = Math.pow(result, 2);
}
total = result/array.length;
int standev = Math.sqrt(total);
return standev;
}**/
}
As you can see, the standard deviation method and its reference in the main method are made into comments. I haven't tried to convert those to array lists yet, since I'm not sure I did the right thing to the others.
Finally, When I run what I have, the compiler doesn't return any errors. Instead, It just hangs, probably having crashed. I'm not sure why. When I use the 'void main(String[] args' option in BlueJ, I submit the name of the text file(Which is in the project directory. theres even one in the main directory just in case that's what's being referenced.
Thank you all.
精彩评论