Homework Help Question & Answers

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
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 Coin

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...

  • JAVA "Class and Driver Class" ~ Almost Finished ~ LIFESAVER!

    Prompt:Design and implement aclass called Dog that contains instance data thatrepresents the dog's name and age. Define theDogconstructor to accept and initialize instance data.Include getter and setter methods for the name andage. Include a method tocompute and return theage of the dog in "person years" (seven times the dog's age).Include a toString method that returns aone-linedescription of thedog. Create adriver class called Kennel, whose main methodinstantiates and updates several Dogobjects. In thedriver class use aloop to get user input for...

  • 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...

  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

  • Inheritance Review:BackToSchool

    LAB ASSIGNMENT A11.1BackToSchoolBackground:The HighSchool application described in the lesson has two classes: the Person superclass and the Student subclass. In this lab you will create two new classes,Teacher and CollegeStudent, using inheritance. A Teacher will be like Person but will have additional properties such as salary (the amount the teacher earns) andsubject (e.g. "Computer Science", "Chemistry", "English", and "Other"). The CollegeStudent class will extend the Student class by adding a year (current level incollege) and major (e.g. "Electrical Engineering", "Communications",...

  • 1. How input stream works: consider the following program that reads the age and name of person...

    1. How input stream works: consider the following program that reads the age and name of person from the keyboard and display them.import java.util.Scanner;public class dataInput{public static void main (String[] args){String name; // Name of the userint age;Scanner scan = new Scanner(System.in);System.out.print ("Enter your age: ");age = scan.nextInt();System.out.print ("Enter your name:");name = scan.nextLine();System.out.println(name + " you are " + age + " years old");}}Type, compile and run the program.a) When it asks for input enter 45 and press enter key....

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

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