I need help with this program
I need to be able to type all the names and the grades then the graph of all the student will be shown but i am only able to type one student name and grade and the graph shows but i can still type student....

am i doing the for looping wrong?

import java.util.Scanner;

public class Students {

    public static void main (final String[] args) {
        final Scanner scanner = new Scanner (System.in);
        final String name = "Name";
        final String result = "Grade";
        final String letter = "Letter";

        // String s1 = scanner.next ();

        // if (scanner.hasNextDouble ()) {

        for (int i = 0; i < 4; i++) {
            final String s3 = scanner.next ();
            final String s2 = scanner.next ();

            final int score = scanner.nextInt ();
            char grade = 0; // "blank final"
            if (score >= 90) {
                grade = 'A';
            } else if (score >= 80) {
                grade = 'B';
            } else if (score >= 70) {
                grade = 'C';
            } else if (score >= 60) {
                grade = 'D';
            } else {
                grade = 'F';

            System.out.printf (" %-10s %5s %10s%n ", name, result, letter);
            System.out.printf ("%-10s %-9d %-10s", s3 + s2, score, grade);
            System.out.println ();


You've got the output section within the for loop. That means that each time through the loop, it gets two Strings and an int, calculates a char for the grade, and prints.
If you end the for loop before the printf statements (move one of the right curly braces up a few lines) your logic will be correct, but you'll have some trouble because you've got your variables declared within the loop.

There are two ways you can deal with that. Since you don't really need to hang on to the data after it's printed - you're not doing any manipulation with it beyond deciding on the grade - you could print the header line before you enter the loop, and then leave the output for each student in the loop.

That's probably the easiest, but it means your input will be visible between each line of output - not what you're looking for.

Well, then if you know you want four students each time, you could make a variable for each value, for each student. "FirstName1", "FirstName2", "FirstName3" and so on. That's a drag, though, and what if you want to change the number of students? You have to make new variables for each of them.

And suppose you wanted to print out the information as you're doing, and then report out the name of the students with the highest, lowest, and most average scores?
To do that, you'd want to keep the data in a structure of some sort. The easiest thing would be to put the data into a set of arrays. This would let you loop through it after you'd finished and print out the information you're interested in, and if you wanted to find the person whose score was closest to the calculated average or whatever else you wanted.

(Pretty soon, you'll learn how to make a Student object and keep all of this stuff together in one place, and then you'll be writing Java for real!)

This article has been dead for over six months. Start a new discussion instead.