-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

4

2 Answers 2

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);
3
  • @Yahya "What if user entered 'Yoo'" The program is fine with that.
    – Tom
    May 29, 2017 at 1: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, 2017 at 1:15
  • Thanks man! Exactly what I was looking for and I learn some new pieces of code. Awesome! May 29, 2017 at 1:44
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.

Your Answer

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

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