-1

Code is this:

import java.util.Scanner;

public class Prelab1 {

public static void main(String[] args)
{
    int programcase = 0;
    Scanner inputscan = new Scanner(System.in);

    System.out.print("Enter case number: ");
    programcase = inputscan.nextInt();

    switch(programcase)
    {
    case 1:
        {
            ////////////////////////////////////////////////////////////////////////////////////

            //WRITE THE CODE OF CASE 1 HERE
            Scanner scan = new Scanner(System.in);

            System.out.println("Enter first student's name: ");
            String name1 = scan.nextLine(); 
            System.out.println("Enter " + name1 + "'s first grade: ");
            int firstGrade = scan.nextInt();
            System.out.println("Enter " + name1 + "'s second lab grade: ");
            int secondGrade = scan.nextInt();
            System.out.println("Enter " + name1 + "'s third lab grade: ");
            int thirdGrade = scan.nextInt();
            System.out.println("Enter " + name1 + "'s fourth lab grade: ");
            int fourthGrade = scan.nextInt();

            System.out.println("Enter second student's name: ");
            int name2 = scan.nextInt();
            System.out.println("Enter " + name2 + "'s first lab grade: ");
            int secondfirstGrade = scan.nextInt();
            System.out.println("Enter " + name2 + "'s second lab grade: ");
            int secondsecondGrade = scan.nextInt();
            System.out.println("Enter " + name2 + "'s third lab grade: ");
            int secondthirdGrade = scan.nextInt();
            System.out.println("Enter " + name2 + "'s fourth lab grade: ");
            int secondfourthGrade = scan.nextInt();

            System.out.println("/+CENG 111 Course Student List+" + "\\");
            System.out.println("Name\t\t\t/Lab1\t/Lab2\t/Lab3\t/Lab4");
            System.out.println(name1 + "\t\t\t/" + firstGrade + "\t/" + secondGrade + "\t/" + thirdGrade + "\t/" + fourthGrade);
            System.out.println(name2 + "\t\t\t/" + secondfirstGrade + "\t/" + secondsecondGrade + "\t/" + secondthirdGrade + "\t/" + secondfourthGrade);

            scan.close();

It continues but it isnt important.When i run it it goes well but in there it says: Enter second students name: Enter second students lab grade:

So it doesnt wait for me to write the name it jumps to the other question.How can i solve this?

eko56
  • 23
  • 4

1 Answers1

-3

That's a misbehaviour of Scanner class. It isn't able to clear its buffer properly, and expects all the inputs to be of same data type.

To solve this, use BufferedReader instead.

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

int a = Integer.parseInt (x.readLine());

String n = x.readLine();
prakhar19
  • 459
  • 5
  • 17
  • "That's a misbehaviour of Scanner class." No, it's not. Your answer is fundamentally wrong. – Seelenvirtuose Nov 06 '15 at 18:08
  • *"It isn't able to clear its buffer properly"* It is able to do that, but only if you're able to use this class properly. Reading the JavaDoc (and maybe some tutorials) should help you here. – Tom Nov 22 '15 at 13:03