Hello everyone,

I need some help understanding some concepts and possibly with my logic on this problem. We are supposed to create a program that asks users for their name, id, and grade average. The set is supposed to be where the exception is thrown. When the user enters their name and it is not a String it needs to throw an error via the try catch method. Same with the other values. I have listed my questions below and will comment out my code to give you an idea of where I am having issues. I really need help, but I by no means want it done for me. I am trying to understand the concepts, so I can understand this class( which I am having a lot of trouble with)

In order to test the student's name as to whether it is a string or not do I use and if statement or simply the try catch? I tried to do this from some examples I have seen floating around, but it is erroring out or returning the wrong output.

Would I use a if else like if its a string then it returns this message or just the try catch?

Can you use multiple try/ catch in one program?

Am I on the right track or completely off base in my code?

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();
		try {
		    int y = Integer.parseInt(tempName);
		catch(NumberFormatException e) {
		    System.out.println("Your name is not a valid. Please enter a string");
		System.out.println("Please enter your id number:");
		int tempId = input.nextInt();

				try {
				    int x = Integer.parseInt(tempId);
				catch(NumberFormatException e) {
				    System.out.println("Your id is not a valid. Please enter an integer");

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



please be gentle lol. I am an artist and coding is just not my forte, but my major requires it unfortunately. Thank you ahead of time for any help.

5 Years
Discussion Span
Last Post by Zaad

Let me put it simply the try catch what you have done for the Id is correct a it would serve the purpose since you are expecting an integer and then if a person enters otherwise is given an exception.

However the way you validate the name is incorrect as if the exception is not thrown that is definitely and integer and does not serve your purpose.
You need to rethink the way you need to validate as even "123" is a String given with the quotes. So validating String would not give a clear picture. So make up what you need to validate ex : Name should contain alphaNumeric characters or name should contain only numeric character. Then you could validate it. However you will not necesarily need a try catch to validate this but if you are strictly asked to only use exception handling then you can throw an exception when the validation fails and catch it which is not required actually.


I need to have someone who can actually help me with the assignment which has to use TRY catch not just tell me that this is not a good way to go about the code.


Zaad gave you a good answer. Maybe you need to read it again. He's explained what you need to do, and why. If you're looking for someone to do it for you then you're in the wrong place.


I need to have someone who can actually help me with the assignment which has to use TRY catch not just tell me that this is not a good way to go about the code.

Everybody in this forum are willing to help as James has said.... But helping does not mean doing your homework for you...

Try on what is explained and if you encounter any problem while doing it of course the forum is there to guide and help you

Edited by Zaad: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.