0

I have a question on what's going on, whenever I try to compile it it keeps giving me an error like this:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Person.main(Person.java:38)

All I want is for the user to be able to input their age and name and have it stored in the "age" and "name" variables, then have it print it out in the bottom string. And if someone would like to help me clean up my code as well, it wouldn't hurt..

import java.util.*; 
import java.io.*; 
import java.util.Scanner;

public class Person 

{

public static void main(String[]args) 

    {

    int age;
    int name;

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter in your age.");
    age = scan.nextInt();

     if (age < 18) 

     {
         System.out.println("So you're a kid, huh? That's fine.");
     } 

     else if (age >= 18)

     {
        System.out.println("Ah, and adult! Good.");
     }

     @SuppressWarnings("resource")
     Scanner in = new Scanner(System.in);

     System.out.println("Enter in your name");
     name = in.nextInt();

     System.out.println("So you're " + age + " years old and your name is " + name);


}
}
0

7 Answers 7

7

Issues

 int name; //Name should be of type String
 ...
 System.out.println("Enter in your name");
 name = in.nextInt(); //It doesn't handle the string since your using `nextInt`

Solution

 String name;
 ...
 System.out.println("Enter in your name");
 name = in.nextLine();
Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that the OP defines name as an int. name should be defined as a string.
1

Why is name an integer? int name;

I suspect you are using alpha characters to input your name...and are getting the exception at this line: name = in.nextInt();

name should not be an integer. It should be a string.

Therefore, string name; and name = in.nextLine();

Comments

0

It means that your program has tried to read a value that as an integer that is not an integer.

when using name = in.nextInt();

it should be string.not int

Comments

0

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source)

Let me explain why you have encountered this type of error You defined name variable as int but in the console you would be giving string as input. So compiler will throw error as InputMismatchException. Better to make name variable as string :)

Comments

0

Corrected Code:

import java.util.*;  
import java.io.*; 

public class Person  { 
    public static void main(String[]args)  {
        int age;
        String name;

        Scanner scan = new Scanner(System.in);

        System.out.println("Enter in your age.");
        age = scan.nextInt();//for Number input 
        System.out.println("Enter in your name");
        name = scan.next();//for String Input 

        if (age < 18) 
        {
             System.out.println("So you're a kid, huh? That's fine.");
        } 

        else if (age >= 18)
        {
            System.out.println("Ah, and adult! Good.");
        }

        System.out.println("So you're " + age + " years old and your name is " + name);

    } 
}

2 Comments

Could you please explain what OP's mistakes were and what you did to fix them?
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

name = in.nextInt(); as you required user name so obviously it will be String type so use either name=in.next() or name=in.nextLine(). i hope it will work now.

1 Comment

This question was asked 7 years before you answer?
-2
import java.util.*;
class Employe
{
    private int id;
    private String name;
    Employe(int id,String name)
    {
        this.id=id;
        this.name=name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public class Quarantine {
public static void main(String[] args) {
    Employe empList[]=new Employe[2];
    Scanner sc=new Scanner(System.in);
    for(int index=0;index<empList.length;index++) {
    int id=sc.nextInt();
    String name=sc.nextLine(); 
    empList[index]=new Employe(id,name);
    }
    for(int index=0;index<empList.length;index++) {
        System.out.println(empList[index].getId()+" "+empList[index].getName());
        }}
}

if I give input as 1, a

it will show error because both are taken as character

I need to give input as 124, a run

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.