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);


}
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Xiam
  • 345
  • 4
  • 8
  • 16

7 Answers7

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();
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    Keep in mind that the OP defines `name` as an `int`. `name` **should** be defined as a `string`. –  Mar 06 '13 at 14:19
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();

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

PSR
  • 39,804
  • 41
  • 111
  • 151
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 :)

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);

    } 
}
Dada
  • 6,313
  • 7
  • 24
  • 43
vinay
  • 71
  • 1
  • 4
  • Could you please explain what OP's mistakes were and what you did to fix them? – Dada Oct 29 '21 at 08:34
  • 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](/help/how-to-answer). – Community Oct 29 '21 at 08:48
-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.

-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

Shrikant Havale
  • 1,250
  • 1
  • 16
  • 36