Here is the assignment criteria :
Implement exception handling to validate user input as was described in class.
You need to at least catch type mismatch exception.
No late or by emailed homework will be accepted.
We are to have a student enter their name, id and grade. The name is a string, grade a float and id an int. This i what I have so far, and I am having a very hard time with this. Please any input would be awesome. Please dumb it down as much as possible I am not a CS major and they force us to take this class for my degree plan.
import java.util.Scanner;
public class tryCatchTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
studentInfo studentInfoObject = new studentInfo();
System.out.println("What is your name?");
String tempName = input.nextLine();
studentInfoObject.setName(tempName);
studentInfoObject.validName();
try {
int y = Integer.parseInt(tempName);
System.out.println(y);
}
catch(NumberFormatException e) {
System.out.println("Your name is not valid. Please enter a string");
}
System.out.println("Please enter your id number:");
int tempId = input.nextInt();
studentInfoObject.setId(tempId);
studentInfoObject.validId();
try {
int x = Integer.parseInt(tempId);
System.out.println(x);
}
catch(NumberFormatException e) {
System.out.println("Your id is not a valid. Please enter a whole number");
}
}
}
public class studentInfo {
private String studentName;
private int studentId;
private float gradeAvg;
public void setName(String name){
studentName = name;
}
public String getName(){
return studentName;
}
public void validName(){
System.out.printf("Your name is being validated %s", getName());
}
public void setId(int id){
studentId = id;
}
public int getId(){
return studentId;
}
public void validId(){
System.out.printf("Your id is being validated %s", getId());
}
public void setAverage(float average){
gradeAvg = average;
}
public float getAverage(){
return gradeAvg;
}
public void validAverage(){
System.out.printf("Your average is being validated %s", getAverage());
}
public studentInfo() {
}
}