Question:
Java Questions Please Help?
anonymous
2009-02-11 10:18:37 UTC
import java.util.*;

public class Stats {
public static int students, studentsTotal, a , max, min ;
public static double average;

public void Input(){
int grades=0;
System.out.println("This program provides statistics (such as min, max, and average)");
System.out.println("following the input of numerical data.");
System.out.println("");
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the total number of integers you'd like to compare:");
students = keyboard.nextInt();
studentsTotal = students;
System.out.println("Enter each number separately, followed by return");
max = keyboard.nextInt();
min=max; //sets initial min and max
students--;
do{
int temp = keyboard.nextInt();
if(temp>max)
max=temp;
if(tempmin=temp;

grades+=temp;
students--;
}while(students>0);
if(studentsTotal<=0)
average = 0;
else
average = (double)grades/studentsTotal;
}
}

I get 2 errors

frankie.udel.edu% javac Stats.java
Stats.java:20: Class Scanner not found.
Scanner keyboard = new Scanner(System.in);
^
Stats.java:20: Class Scanner not found.
Scanner keyboard = new Scanner(System.in);

Since Scanner is kind of new it does not work in unix terminal.

can any one make this code work the way why but using something other than Scanner. Im not sure if BufferReader would work

Thank you in advance
Four answers:
FXJKHR
2009-02-11 10:36:02 UTC
As far as I can see in the Java API, the java.util.Scanner was added in 1.5.0, so if you are using 1.4.2 or earlier, I'd recommend that you upgrade your Java SDK version.



Try "javac -version 2>&1|head -n 1" to see which version you are using.
deonejuan
2009-02-11 19:14:01 UTC
for what it's worth, here is similar functionality using BufferedReader.



import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Arrays;



public class StudentStats {



static private void title() {

System.out.println("This program provides statistics (such as min, max, and average)");

System.out.println("following the input of numerical data.");

System.out.println("");



}



public static void main(String[] args) throws IOException {

BufferedReader cin =

new BufferedReader(new InputStreamReader(System.in));

title();



String number;

System.out.println("Enter the total number of integers you'd like to compare: ");

int total = 0;

while ((number = cin.readLine()) != null) {

try {

total = Integer.parseInt(number);

} catch (NumberFormatException e) {

System.err.println("Invalid number in input");

System.exit(1);

}

String inputNumbers = "";

for (int i = 0; i < total; i++) {

System.out.println("Enter number "+(i+1)+": ");

inputNumbers += cin.readLine() +",";

}

String[] arrayStringNums = inputNumbers.split(",");

double[] numbersDouble = new double[arrayStringNums.length];

double numDblTot = 0.0;

for (int i = 0; i < numbersDouble.length; i++) {

numbersDouble[i] = Double.parseDouble(arrayStringNums[i]);

numDblTot += numbersDouble[i];

}

System.out.println("The total: " + numDblTot);

Arrays.sort( numbersDouble);

System.out.println("Your minimum: " + numbersDouble[0]);

System.out.println("Your max: "+ numbersDouble[numbersDouble.length-1]);

System.out.println("Your avg: "+(numDblTot / numbersDouble.length));

}





}

}



In a console on your Unix station. type

java -version

and tell us what it says



this could all have been avoided if you had changed the top of the import to:

import java.util.Scanner; // pass or fail if your Unix had it
Harsha
2009-02-11 18:30:28 UTC
try to import java.io package
anonymous
2009-02-11 18:33:13 UTC
webdesign software

http://cofecup.info/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...