tags:

views:

136

answers:

5

I'm trying to calculate the average of a student's marks:

import java.util.Scanner;

public class Average

{

    public static void main(String[] args)
    {
        int mark;
        int countTotal = 0;  // to count the number of entered marks
        int avg = 0;        // to calculate the total average
        Scanner Scan = new Scanner(System.in);

        System.out.print("Enter your marks: ");
        String Name = Scan.next();

        while (Scan.hasNextInt())
        {
            mark = Scan.nextInt();
            countTotal++;

            avg = avg + ((mark - avg) / countTotal);
        }


        System.out.print( Name + "  " + avg );
    } 
}
+4  A: 

Because it's still sitting in the while loop, waiting for an instruction to break out of the loop.

Here's a Sun tutorial on the subject. The course materials you got should also contain this information by the way.

BalusC
+1  A: 

Hi WM,

In my previous post (previous answer) I indicated that you need to press Ctrl+D after you are done inputting numbers. Ctrl+D signals to your program that there is no more input.

Try entering:

WM 1 2 3 6

Then press Ctrl+D

Chris J
That's more a workaround than a solution. I'm not sure if he get extra points for that.
BalusC
yea @Balus is right,, and btw it didnt work out,, i want it to work when pressing enter but still not printing..
WM
You'll need to change your `while` statement to break on an empty new line. Check the methods provided by `Scanner`. Or consult your course materials, tutor and/or classmates. There they are for.
BalusC
@WM: read my answer again, especially the hint about `Scanner` scanning a string which is a line from another `Scanner`.
polygenelubricants
i didnt get what you stated sorry,,
WM
@WM: see my answer here on this question http://stackoverflow.com/questions/2753876/why-is-my-code-not-printing-anything-to-stdout/2753964#2753964
polygenelubricants
A: 

You should use getline and accept an "exit" string. String.split and some of the Integer.valueOf stuff makes parsing numbers from string literals written in very easy in Java.

DeadMG
+3  A: 

Here's a solution that uses two Scanner (as suggested in my previous answer).

  • Scanner stdin = new Scanner(System.in); scans user's input
  • Scanner scores = new Scanner(stdin.nextLine()); scans the line containing the scores

Note also that it uses a much simpler and more readable formula for computing the average.

        Scanner stdin = new Scanner(System.in);

        System.out.print("Enter your average: ");
        String name = stdin.next();

        int count = 0;
        int sum = 0;
        Scanner scores = new Scanner(stdin.nextLine());
        while (scores.hasNextInt()) {
            sum += scores.nextInt();
            count++;
        }
        double avg = 1D * sum / count;
        System.out.print(name + "  " + avg);

Sample output:

Enter your average: Joe 1 2 3
Joe  2.0
polygenelubricants
I should chagne `System.out.print(name + ....);` to `println`
Martijn Courteaux
The average formula I gave WM is the same one used in apache math:http://commons.apache.org/math/api-1.0/org/apache/commons/math/stat/descriptive/moment/Mean.html
Chris J
A: 

How about this?

import java.util.Scanner;

public class Average{

public static void main(String[] args)
{
    int mark = 0;
    int countTotal = 0;  // to count the number of entered marks
    int avg = 0;        // to calculate the total average
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter student name: ");
    String name = scan.next();

    System.out.println("Please enter marks one by one?");


    while(scan.hasNextInt()){

        int tempMark = scan.nextInt();
        mark += tempMark;
        countTotal+=1;

        System.out.println("If you are done with mark, type \"Done!\"");
    }

    avg = mark/countTotal;

    System.out.print( name + "  " + avg );
} 

}