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, 2022 in Java by Aditya
• 7,680 points
2,061 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, 2022 by Rahul
• 9,680 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, 2022 in Java by narikkadan
• 63,600 points
5,525 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
1,851 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
24,964 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 81,223 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
8,359 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
17,433 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

edited Aug 4, 2023 by Khan Sarfaraz 4,876 views
0 votes
1 answer

How to split a string in Java

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

answered Feb 9, 2022 in Java by Rahul
• 9,680 points

edited Jul 6, 2023 by Khan Sarfaraz 741 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP