Question

Person Name: John Doe Person Birth Year: 1960 Person Name: Emily Zhang Person Birth Year: 2007...

Person Name: John Doe

Person Birth Year: 1960

Person Name: Emily Zhang

Person Birth Year: 2007

Student Major: Finance

Person Name: Paul zhang

Person Birth Year: 1970

Instructor Salary: $50,000.00

Exception in thread "main" java.util.InputMismatchException

at java.base/java.util.Scanner.throwFor(Scanner.java:939)

at java.base/java.util.Scanner.next(Scanner.java:1594)

at java.base/java.util.Scanner.nextInt(Scanner.java:2258)

at java.base/java.util.Scanner.nextInt(Scanner.java:2212)

at PersonTest.main(PersonTest.java:47)

import java.util.Scanner;

public class PersonTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Person Name: ");

String student_name = scan.nextLine();

System.out.print("Person Birth Year: ");

int student_yearOfBirth = scan.nextInt();

Person p = new Person(student_name, student_yearOfBirth);

scan.nextLine();

System.out.print("Person Name: ");

student_name = scan.nextLine();

System.out.print("Person Birth Year: ");

student_yearOfBirth = scan.nextInt();

scan.nextLine();

System.out.print("Student Major: ");

String student_major = scan.nextLine();

Student s = new Student(student_name, student_yearOfBirth, student_major);

System.out.print("Person Name: ");

String instructor_name = scan.nextLine();

System.out.print("Person Birth Year: ");

int instructor_yearOfBirth = scan.nextInt();

System.out.print("Instructor Salary: ");

int instructor_salary = scan.nextInt();

Instructor i = new Instructor(instructor_name, instructor_yearOfBirth, instructor_salary);

System.out.println(s.toString());

System.out.println(i.toString());

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #0

I do not have your instructor and student classes, So i can not run and test the code.. But i can explain why you are getting the error.

The Scanner's class nextInt method expects an integer to be read, to contain only digits.. no comma and dollar signs.. As you gave the input as $50,000.00, it failed.. What you should do is to give it only digits as the input.. and it will work fine..

In case, you still want to input the salary with dollar and comma sign, you then need to read it as string first, remove the dollar and comma sign your self, then using Integer.parseInt method, you can convert the string to integer.

hope it helps! let me know if any issues.

Add a comment
Know the answer?
Add Answer to:
Person Name: John Doe Person Birth Year: 1960 Person Name: Emily Zhang Person Birth Year: 2007...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • write a program which include a class containing an array of words (strings).The program will search...

    write a program which include a class containing an array of words (strings).The program will search the array for a specific word. if it finds the word:it will return a true value.if the array does not contain the word. it will return a false value. enhancements: make the array off words dynamic, so that the use is prompter to enter the list of words. make the word searcher for dynamic, so that a different word can be searched for each...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • design two java classes The example of output would be this: user puts the amount of...

    design two java classes The example of output would be this: user puts the amount of money: (for example 9000) I'm going to get three things 1. Cellphone cost: 3000 2. Laptop cost: 6000 3. Macbook cost: 4000 From now on, my balance would be (+balance) and I bought cellphone, laptop, (since there is not enough amount of money, I can only buy cellphone and laptop. if there is enough money then it can buy all things) so I have...

  • You will create a class to keep student's information: name, student ID, and grade. The program...

    You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality: - The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change. - The program should provide the option to create a new entry with a student's name, ID, and grade. - There should be an option to lookup a student from his student ID (this will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT