3

I have the following method:

public void addStudent(){

    String fName, lName;
    double mGrade, fGrade;
    System.out.print("\nEnter first name: ");
    Scanner in = new Scanner(System.in);
    fName = in.nextLine();
    System.out.print("\nEnter last name: ");
    lName = in.nextLine();
    System.out.print("\nEnter midterm grade: ");
    mGrade = in.nextDouble();
    System.out.print("\nEnter final grade: ");
    fGrade = in.nextDouble();
    Student toAdd = new Student(fName, lName, mGrade, fGrade);
    students.add(toAdd);
    System.out.println("\nStudent record added.\n");
    System.out.println(students.size());
}

How can I check if the user typed in something other than an integer for midterm grade and final grade? And if they entered a non-integer, I want the method to just request the user type in that value again. I'm guessing I'll have to use a do-while loop. But I don't don't know how to check the type...

Thanks!

2

5 Answers 5

2

You can use Scanner.next() and try to parse it to the type you want (ex. to integer by using Integer.parseInt(x) and if it fails (throws and exception) try to do it again.

Sign up to request clarification or add additional context in comments.

2 Comments

er, i messed up in my original post. can i parse a double?
Yes you can, you can do that by using Double.parseDouble(x)
1

Yes, a do-while will work best.

  int midterm;
  System.out.printLn("Enter midterm grade");
  do
  { 
      try {
          string s = in.nextLine();
          midterm = Integer.parseInt(s);
          break;
      }
      catch (Exception e)
      {
          System.out.printLn("Couldn't parse input, please try again");
      }
  }
  while (true);

2 Comments

er, i messed up in my description...can i parse a double?
Absolutely, instead of integer, use Double.parse.
1

You may use the method: nextInt() from Scanner

Alternatively you can check if a string is an integer like this:

if( someString.matches("\\d+") ) {
  // it is 
} else {
 // it isn't 
}

1 Comment

Thanks, this works beautifully for a similar issue I was having. I much prefer this to making a try/catch block.
1

Run the input method in a loop, if user enters something other than valid integer or double, repeat asking for the input.

Comments

1

you can try this

import java.io.*;
import java.util.Scanner;
public class Test {
    public static void main(String args[]) throws Exception {

        String input;
        int ch1;
        float ch2;
        String ch3;

        Scanner one = new Scanner(System.in);

        input = one.nextLine();

        try {
            ch1 = Integer.parseInt(input);
            System.out.println("integer");
            return;
        } catch (NumberFormatException e) {


        }

        try {
            ch2 = Float.parseFloat(input);
            System.out.println("float");
            return;
        } catch (NumberFormatException e) {

        }
        try {
            ch3 = String.valueOf(input);
            System.out.println("String");
        } catch (NumberFormatException e) {

        }


    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.