-3

First off, I'm sorry if I am making a duplicate post. I tried looking for the solution and could not find it. I'm making a grade calculator where the user inputs a double "x" amount of times via a scanner. I've got the basic fundamentals of it down, and I'm not trying to fix any issues that a user might have when inputting numbers.

public static void main(String args[]) {

    double total = 0;
    int counter = 0;

    ArrayList<String> answerYes = new ArrayList<>();
    answerYes.add("yes");
    answerYes.add("y");
    answerYes.add("yea");


    Scanner answerCheck = new Scanner(System.in);
    System.out.println("Would you like to submit a number to calculate the average? [y/n]");
    String userInput = answerCheck.nextLine();
    while (answerYes.contains(userInput)) {
        Scanner numberInput = new Scanner(System.in);
        System.out.println("Please input a number: ");
        Integer number = numberInput.nextInt(); //Here is where I need to check for a non-integer.
        total += number;
        System.out.println("Would you like to submit another number to calculate the average? [y/n]");
        userInput = answerCheck.nextLine();
        counter++;
    }
    double average = total/counter;
    System.out.println("The average of those numbers is: " + average);

}

I'm pretty certain I made this more complicated than this had to be, but I wanted to test my ability to make an average calculator the way I would without the internet. Hopefully I formatted this correctly.

Thanks, Jordan

JordanMorris
  • 13
  • 1
  • 5
  • 2
    Possible duplicate of [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom May 29 '17 at 01:03
  • 2
    `nextInt()` will only ever read *a number*, so you have to use a different method – OneCricketeer May 29 '17 at 01:03
  • 1
    See [this post](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) to know why your current code doesn't work as expected – OneCricketeer May 29 '17 at 01:05
  • I think this answer may help you https://stackoverflow.com/questions/43595786/how-do-i-use-stop-as-a-keyword-to-stop-the-for-loop/43596011#43596011 – Yahya May 29 '17 at 01:08

2 Answers2

1

I think what you're looking to do is something like this.

try {
    int input = scanner.nextInt();
    // remaining logic
} catch (InputMismatchException e) {
    System.out.println("uh oh");
}

So if the user enters something which can't be read as an integer it will throw a InputMismatchException.

You could extend this by putting it in a loop forcing the user to enter a number before continuing.

Kevin
  • 380
  • 7
  • 14
1

You only need one Scanner, and you can use String.startsWith instead of checking against a collection. Something like,

double total = 0;
int counter = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to submit a number to calculate the average? [y/n]");
String userInput = scan.nextLine();
while (userInput.toLowerCase().startsWith("y")) {
    System.out.println("Please input a number: ");
    if (scan.hasNextInt()) {
        total += scan.nextInt();
        counter++;
    }
    scan.nextLine();
    System.out.println("Would you like to submit another number to calculate the average? [y/n]");
    userInput = scan.nextLine();
}
double average = total / counter;
System.out.println("The average of those numbers is: " + average);
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
  • @Yahya *"What if user entered 'Yoo'"* The program is fine with that. – Tom May 29 '17 at 01:12
  • @Tom *"The program is fine with that"*, The program will execute the `while-loop` block (i.e will take the user to enter a number). – Yahya May 29 '17 at 01:15
  • Thanks man! Exactly what I was looking for and I learn some new pieces of code. Awesome! – JordanMorris May 29 '17 at 01:44