Question: Find the bug in the Java Code: import javax.swing.JOptionPane; public class Week7_Debug { public static void main(String[] args) { Person person = new Person(); String

Find the bug in the Java Code:

import javax.swing.JOptionPane;

public class Week7_Debug {

public static void main(String[] args) {

Person person = new Person();

String firstName = JOptionPane.showInputDialog("Please enter a first name");

String lastName = JOptionPane.showInputDialog("Please enter a last name");

int age;

while (true)

{

String input = JOptionPane.showInputDialog("Please enter an age for the person");

try {

age = Integer.parseInt(input);

break;

}

catch (Exception e){

JOptionPane.showMessageDialog(null, "Invalid age entered. Try Again");

}

}

person.setFirstName(firstName);

person.setLastName(lastName);

person.setAge(age);

}

}

class Person {

private String firstName;

private String lastName;

private int age;

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Person(String firstName, String lastName, int age) {

super();

this.firstName = firstName;

this.lastName = lastName;

this.age = age;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!