0

So I created a class called student and I wanted to define myself using a scanner to fullfill all the parameters i.e Age name etc and I got a problem when it gets to fullfill one parameter. I dont't know why when I enter the gpa it automatically go to Age by skiping the Major. So I guess I've a problem when I use the scanner :/

Thanks in advance.

Here's the code ...

import java.util.Scanner;

public class ClassObject {

public static void main(String[] args) {
     
    Student a = new Student();
    Scanner student = null;
    
    try  {
        student = new Scanner(System.in);
        
        System.out.println("First Name --> ");
        a.firstName = student.nextLine();
        
        System.out.println("Last Name --> ");
        a.lastName = student.nextLine();
        
        System.out.println("GPA --> ");
        a.gpa = student.nextDouble();
        
        System.out.println("Major --> ");
        a.major = student.nextLine();
        
        System.out.println("Age --> ");
        a.age = student.nextInt();
        
        System.out.println("Are you on probation --> ");
        a.onProbation = student.nextBoolean();
        
    } finally {
        
        if(student != null)
        {
            student.close();
            
        }
    }
    
    
            
    

    
}

}

Here's is the student class

public class Student {

String firstName;
String lastName;
double gpa;
String major;
int age;
boolean onProbation;

}

Anas
  • 1
  • Maybe you're giving an int instead of double in the input? Since you're using scanner.nextDouble() for the GPA and scanner.nextInt() for the Age – Jorn Rigter Apr 24 '21 at 10:24

1 Answers1

-1
student.nextLine();
student.nextDouble();
student.nextInt();
student.nextBoolean();

Can not be mixed to use.

Change all to student.nextLine();

All code like this:

import java.util.Scanner;

public class ClassObject {

public static void main(String[] args) {
     
    Student a = new Student();
    Scanner student = null;
    
    try  {
        student = new Scanner(System.in);
        
        System.out.println("First Name --> ");
        a.firstName = student.nextLine();
        
        System.out.println("Last Name --> ");
        a.lastName = student.nextLine();
        
        System.out.println("GPA --> ");
        a.gpa = Double.parseDouble(student.nextLine());
        
        System.out.println("Major --> ");
        a.major = student.nextLine();
        
        System.out.println("Age --> ");
        a.age = Integer.parseInt(student.nextLine());
        
        System.out.println("Are you on probation --> ");
        a.onProbation = Boolean.parseBoolean(student.nextLine());
        
    } finally {
        
        if(student != null)
        {
            student.close();
            
        }
    }
}
timhu
  • 98
  • 4
  • Well, they can be mixed, it's just that you need to be aware of the behavior of the methods. The answers in the linked question explain this very well. – MC Emperor Apr 24 '21 at 10:37