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() {
		

	
	
	}

}

Sorry for posting twice i redid the code so i thought it was okay. i am looking for help not just someone to tell me i am wrong or i don't know what i am doing. OBVIOUSLY i don't otherwise I WOULD NOT be here. I was under the impression that this was a place for help and learning. I must be mistaken. All I have gotten thus far is people flaming threads or being antagonistic instead of providing any real help or explanation.

OK, if you want help...

Why do you think that using Integer.parseInt() will verify that the input is a string? When do you think that Integer.parseInt() would throw an exception? Would an input "Mimi" is an acceptable string name for you? If it is, then you are off the track for checking input string with Integer.parseInt(). Otherwise, you are on the right track. You may not be able to use try-catch to check a String as name, you could however use matches() method of String itself to help you verify the input string.

For the part of ID number, it is suitable to use Integer.parseInt() for the check if and only if you are expecting only a number entered.

Hi Taywin.
Don't know if you spotted the earlier thread on the same subject, but Zaad already explained all that, but the OP didn't see that as helpful. :(

James,

Yes, I did. But I guess that the OP didn't understand what Zaad explain. I am trying to explain it using different approach. Not sure the OP would understand it either. :(

OK man. Just didn't want anyone to waste their time with a duplicate post. No worries. J.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.