import java.io.*;
import java.util.*;
public class prog1 {
public static void main(String[] args) {
Scanner in = new Scanner(new File("prog0.dat"));
int[] list = in.nextInt;
java.util.Arrays.sort(list);
System.out.print("Sorted list: " );
for (int i=0; i<list.length; i++)
System.out.print(list[i] + " ");
System.out.println("\nThe lowest grade is " + minIndex(list));
System.out.println("The highest grade is " + maxIndex(list));
System.out.println("The mean is " + mean(list));
System.out.println("The standard deviation is " + standardDeviation(list));
System.out.println("The mode is " + mode(list));
System.out.println("The range is " + range(list));
System.out.println("The median is " + median(list));
}
public static int minIndex(int[] list) {
int min = list[0];
int minIndex = 0;
for (int i = 1; i < list.length; i++)
if (min > list[i]) {
min = list[i];
minIndex = i;
}
return min;
}
public static int maxIndex(int[] list) {
int max = list[0];
int maxIndex = 0;
for (int i = 1; i < list.length; i++)
if (max < list[i]) {
max = list[i];
maxIndex = i;
}
return max;
}
public static int mean(int[] list) {
int sum = 0;
for (int i=0; i<list.length; i++) {
sum += list[i];
}
return sum / list.length;
}
public static double standardDeviation (int[] list) {
double[] arr = new double[list.length];
double av = mean(list);
for (int i = 0; i <list.length; i++) {
arr[i] = (list[i] - av) * (list[i] - av);
}
double d = 0;
for (int i = 0; i < list.length; i++) {
d = d + arr[i];
}
return Math.sqrt(d / (arr.length - 1));
}
public static int mode(int[] list) {
int maxValue = 0;
int maxCount = 0;
for (int i = 0; i < list.length; ++i) {
int count = 0;
for (int j = 0; j < list.length; ++j) {
if (list[j] == list[i]) ++count;
}
if (count > maxCount) {
maxCount = count;
maxValue = list[i];
}
}
return maxValue;
}
public static int range(int[] list) {
int max, min, range;
max = list[list.length - 1];
min = list[0];
range = max - min;
return range;
}
private static int median(int[] list) {
int med;
if ((list.length % 2) == 0) {
med = (list[list.length/2] + list[list.length/2 - 1]) / 2;
} else {
med = list[list.length / 2];
}
return med;
}
}
ScannerTyring to figure out how to read in a file to my array properly
Page 1 of 1
8 Replies - 639 Views - Last Post: 14 September 2010 - 07:40 PM
#1
Scanner
Posted 14 September 2010 - 06:03 PM
Im trying to read a file into the scanner class. I am also trying to assign values to the file using in.next...Im having a problem with it.
Replies To: Scanner
#2
Re: Scanner
Posted 14 September 2010 - 06:14 PM
Remember, all methods have parentheses, and nextInt() is a method. Also, nextInt() returns a single int, not an array. Once you declare and initialize the array, you can assign the int you read in to list[someIndex].
Beyond that, what specifically are your problems or errors?
Beyond that, what specifically are your problems or errors?
#3
Re: Scanner
Posted 14 September 2010 - 06:19 PM
I'm not trying to hijack the thread but I noticed that the OP was trying to read from a dat file and it made me wonder what all types of files the Scanner can handle. Anyone got a link with a list of valid file types?
Thanks
Thanks
This post has been edited by Codebug: 14 September 2010 - 06:19 PM
#4
Re: Scanner
Posted 14 September 2010 - 06:21 PM
File type doesn't matter for any of the Java file reading tools, including Scanner. At the root, all files have some sort of String data that can be parsed, which is the key.
#5
Re: Scanner
Posted 14 September 2010 - 06:31 PM
This is the only error Im getting
cannot find symbol variable nextInt
cannot find symbol variable nextInt
#6
Re: Scanner
Posted 14 September 2010 - 06:32 PM
As our good friend the macosxnerd pointed out in his first reply, nextInt is a method, and when you use it, you need to include the parenthesis at the end of it. nextInt()
#7
Re: Scanner
Posted 14 September 2010 - 06:42 PM
What about this
G:\CSC 202\prog1.java:14: incompatible types
found : int
required: int[]
int[] list = in.nextInt();
^
1 error
Process completed.
G:\CSC 202\prog1.java:14: incompatible types
found : int
required: int[]
int[] list = in.nextInt();
^
1 error
Process completed.
#8
Re: Scanner
Posted 14 September 2010 - 06:46 PM
macosxnerd101, on 14 September 2010 - 09:14 PM, said:
Remember, all methods have parentheses, and nextInt() is a method. Also, nextInt() returns a single int, not an array. Once you declare and initialize the array, you can assign the int you read in to list[someIndex].
Beyond that, what specifically are your problems or errors?
Beyond that, what specifically are your problems or errors?
I covered that as well. Did you read my post?
#9
Re: Scanner
Posted 14 September 2010 - 07:40 PM
If you want help, please consider reading our members' posts. We're all here to try to help you. If you don't understand ANYTHING that was said, anything at all, feel free to ask and we will clarify further. There's no shame in it and I do it all the time.
*hint* Look at post #2
*hint* Look at post #2
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote










|