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.

2
  • er, i messed up in my original post. can i parse a double?
    – user618712
    Mar 16, 2011 at 23:54
  • Yes you can, you can do that by using Double.parseDouble(x)
    – Argote
    Mar 17, 2011 at 0:29
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
  • er, i messed up in my description...can i parse a double?
    – user618712
    Mar 16, 2011 at 23:53
  • Absolutely, instead of integer, use Double.parse. Mar 17, 2011 at 1:08
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
  • 1
    Thanks, this works beautifully for a similar issue I was having. I much prefer this to making a try/catch block.
    – Jae Carr
    May 7, 2013 at 15:31
1

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

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

        }


    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.