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!

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
user618712
  • 1,463
  • 7
  • 17
  • 19
  • possible duplicate of [What's the best way to check to see if a String represents an integer in Java?](http://stackoverflow.com/questions/237159/whats-the-best-way-to-check-to-see-if-a-string-represents-an-integer-in-java) – Mike Samuel Mar 16 '11 at 23:42
  • @mike - thanks, that might help. i'm not allowed to add methods for this problem though. – user618712 Mar 16 '11 at 23:46

5 Answers5

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.

Argote
  • 2,155
  • 1
  • 15
  • 20
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) {

        }


    }
}
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 
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 1
    Thanks, this works beautifully for a similar issue I was having. I much prefer this to making a try/catch block. – Jay Carr May 07 '13 at 15:31
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);
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
1

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

Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55