-7

I have an "Exception in thread" error. I create a public class student and give it three classes: NAME of student, ID of student and GPA of student. When I run the code and enter information for student1 it's good. But when I enter student2, the code skips the name for student2, and gives me the student id and GPA. How can I remedy this?

output exception

Here is the code:

import java.util.Scanner;
class Student {

    public String name;
    public int id;
    public float gpa;
}
public class learning {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Student s1 = new Student();
        System.out.print("Enter your name: ");
        s1.name = input.nextLine();
        System.out.print("Enter your id: ");
        s1.id = input.nextInt();
        System.out.print("Enter your gpa: ");
        s1.gpa = input.nextFloat();


        Student s2 = new Student();
        System.out.print("Enter your name: ");
        s2.name = input.nextLine();
        System.out.print("Enter your id: ");
        s2.id = input.nextInt();
        System.out.print("Enter your gpa: ");
        s2.gpa = input.nextFloat();


        Student s3 = new Student();
        System.out.print("Enter your name: ");
        s3.name = input.nextLine();
        System.out.print("Enter your id: ");
        s3.id = input.nextInt();
        System.out.print("Enter your gpa: ");
        s3.gpa = input.nextFloat();

        System.out.println("your name: " + s1.name + "\n"
                + "your id: " + s1.id + "\n"
                + "your GPA: " + s1.gpa);

    }

}
ekhumoro
  • 98,079
  • 17
  • 183
  • 279
  • 1
    Welcome to Stackoverflow, please read [How To Ask](https://stackoverflow.com/help/how-to-ask). Pay special attention to [How To Create MCVE](https://stackoverflow.com/help/mcve). The more effort you'll put into posting a good question: one which is easy to read, understand and which is [on topic](https://stackoverflow.com/help/on-topic) - the chances are higher that it will attract the relevant people and you'll get help even faster. Good luck! – Nir Alfasi Nov 11 '17 at 15:44
  • Show your code for us to figure out what's going on – VHS Nov 11 '17 at 15:44
  • check it here please https://pastebin.com/grCEuM9w –  Nov 11 '17 at 16:02
  • use the [edit] functionality and provide the code inside the question, not on some external site. – luk2302 Nov 11 '17 at 16:38
  • and you are still missing any kind of problem description. – luk2302 Nov 11 '17 at 16:45
  • Done, This is the first time I was ask on stack overflow ^_^ –  Nov 11 '17 at 17:55

1 Answers1

1

Your scanner is getting screwed up if you both use nextLine and next/nextInt/nextDouble refer to this for more Information.

I changed your main method accordingly, it now works like you wanted it to.

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Student s1 = new Student();
    System.out.print("Enter your name: ");
    s1.name = input.nextLine();
    System.out.print("Enter your id: ");
    s1.id = Integer.parseInt(input.nextLine());
    System.out.print("Enter your gpa: ");
    s1.gpa = Float.parseFloat(input.nextLine());

    Student s2 = new Student();
    System.out.print("Enter your name: ");
    s2.name = input.nextLine();
    System.out.print("Enter your id: ");
    s2.id = Integer.parseInt(input.nextLine());
    System.out.print("Enter your gpa: ");
    s2.gpa = Float.parseFloat(input.nextLine());

    Student s3 = new Student();
    System.out.print("Enter your name: ");
    s3.name = input.nextLine();
    System.out.print("Enter your id: ");
    s3.id = Integer.parseInt(input.nextLine());
    System.out.print("Enter your gpa: ");
    s3.gpa = Float.parseFloat(input.nextLine());

    System.out.println("your name: " + s1.name + "\n" + "your id: " + s1.id + "\n" + "your GPA: " + s1.gpa);

}
Naeramarth
  • 106
  • 13