Exception in thread main java util InputMismatchException error

0 votes

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

}

}

Feb 18 in Java by Aditya
• 7,280 points
366 views

1 answer to this question.

0 votes
Problem

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

answered Feb 18 by Rahul
• 8,980 points

Related Questions In Java

0 votes
0 answers

Exception in thread "main" java.lang.Error: Unresolved compilation problems

I am facing issues importing classes and ...READ MORE

May 7 in Java by narikkadan
• 10,840 points
426 views
0 votes
0 answers

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication at java.base/java.lang.ClassLoader.defineClass1(Native ...READ MORE

Oct 22, 2021 in Java by anonymous
• 120 points
597 views
0 votes
2 answers
0 votes
1 answer

How can you catch an exception thrown by another thread in Java?

This can be done using Thread.UncaughtExceptionHandler. Here’s a simple ...READ MORE

answered Dec 3, 2018 in Java by Frankie
• 9,830 points
18,068 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 60,815 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
6,648 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
14,199 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
3,040 views
0 votes
1 answer

How to split a string in Java

By using the appropriate method, this can ...READ MORE

answered Feb 9 in Java by Rahul
• 8,980 points
36 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP