-3

I am trying to write a program which asks for 6 students' names and grades, and calculates it (A, B, C, F etc..)

I have an annoying bug that I cannot solve. If you run it and see the output you will know what I mean: it skips asking for the second student name.

import java.util.*;

public class grading {
    public static void main (String [] args) {
        Scanner kb = new Scanner (System.in);
        String a, b, c, d, e, f = "";
        int mid1, mid2, finl, total = 0;
        int mid12, mid22, finl2, total2 = 0;
        int mid13, mid23, finl3, total3 = 0;
        int mid14, mid24, finl4, total4 = 0;
        int mid15, mid25, finl5, total5 = 0;
        int mid16, mid26, finl6, total6 = 0;
        String g1, g2, g3, g4, g5, g6 = "0";

        System.out.println ("Enter you first student name :");
        a = kb.nextLine();

        System.out.println ("His first midterm grade [out of 25] :");
        mid1 = kb.nextInt();

        System.out.println ("His second midterm grade [out of 25] :");
        mid2 = kb.nextInt();

        System.out.println ("His final test grade [out of 50] :");
        finl = kb.nextInt();

        total = mid1 + mid2 + finl;

        ///////////////////////// Student 1

        System.out.println ("Enter you second student name :");
        b = kb.nextLine();

        System.out.println ("His first midterm grade [out of 25] :");
        mid12 = kb.nextInt();

        System.out.println ("His second midterm grade [out of 25] :");
        mid22 = kb.nextInt();

        System.out.println ("His final test grade [out of 50] :");
        finl2 = kb.nextInt();

        total2 = mid12 + mid22 + finl2;

        ////////////////////// Student 2

        System.out.println ("Enter you third student name :");
        c = kb.nextLine();

        System.out.println ("His first midterm grade [out of 25] :");
        mid13 = kb.nextInt();

        System.out.println ("His second midterm grade [out of 25] :");
        mid23 = kb.nextInt();

        System.out.println ("His final test grade [out of 50] :");
        finl3 = kb.nextInt();

        total3 = mid13 + mid23 + finl3;

        ///////////////////// Student 3

        System.out.println ("Enter you fourth student name :");
        d = kb.nextLine();

        System.out.println ("His first midterm grade [out of 25] :");
        mid14 = kb.nextInt();

        System.out.println ("His second midterm grade [out of 25] :");
        mid24 = kb.nextInt();

        System.out.println ("His final test grade [out of 50] :");
        finl4 = kb.nextInt();

        total4 = mid14 + mid24 + finl4;

        //////////////////////// Student 4

        System.out.println ("Enter you fifth student name :");
        e = kb.nextLine();

        System.out.println ("His first midterm grade [out of 25] :");
        mid15 = kb.nextInt();

        System.out.println ("His second midterm grade [out of 25] :");
        mid25 = kb.nextInt();

        System.out.println ("His final test grade [out of 50] :");
        finl5 = kb.nextInt();

        total5 = mid15 + mid25 + finl5;

        /////////////////// Student 5


        System.out.println ("Enter you sixth student name :");
        f = kb.nextLine();

        System.out.println ("His first midterm grade [out of 25] :");
        mid16 = kb.nextInt();

        System.out.println ("His second midterm grade [out of 25] :");
        mid26 = kb.nextInt();

        System.out.println ("His final test grade [out of 50] :");
        finl6 = kb.nextInt();

        total6 = mid16 + mid26 + finl6;

        /////////////// Student 6 
    }
}
khelwood
  • 46,621
  • 12
  • 59
  • 83

3 Answers3

0

Your input consists of ints and lines. nextInt will give you the next int supplied, consuming a newline if it needs to. However if you follow a nextInt with a newLine, the newline will be satisfied with carriage return used to input the int.

The solution is to add

kb.nextLine();

after reading the final grade for each student.

Neil Masson
  • 2,499
  • 1
  • 13
  • 22
0

Another solution would be to use a new Scanner for every reading.

a = new Scanner (System.in).nextLine();
mid1 = new Scanner (System.in).nextInt();
mid2 = new Scanner (System.in).nextInt();
finl = new Scanner (System.in).nextInt();
b = new Scanner (System.in).nextLine();
...
Tobias Johansson
  • 368
  • 3
  • 11
0

Try using kb.next() instead of kb.nextLine(). That should work.

When you press enter after typing the int value, since it is not an integer, it gets consumed as the value for your kb.nextLine() so it is skipped.

Uma
  • 40
  • 2
  • 10