-3

I'm trying to write a program that asks the user if they want to enter a real number. If yes, then prompt the user to enter a number. Keep prompting for number inputs until user says "no." Once this happens, output the average of the numbers entered.

I think I'm stuck trying to implement a string for the sentinel value. I want the sentinel value to be "no." But with this last attempt I have here, when I enter "yes" I get an InputMismatchException. Also, not sure if this is a job for do/while or just while. Kind of new to all this and not sure how to go about this, as we don't have examples of using strings for sentinels.

public static void main(String[] args) {

int count = 0;
float total = 0;        
float inputNumber;


Scanner scan = new Scanner ( System.in );

System.out.println("Want to enter a number?");

String reply = "";


inputNumber = scan.nextFloat();

do {
    reply = scan.nextLine();
    if (reply.equalsIgnoreCase("yes")) {
        System.out.println("Enter a number > ");

        total+= inputNumber ;
        count++ ;

        System.out.println("Enter another number, or " +
                "enter \"no\" to terminate > " );
        inputNumber = scan.nextFloat(); 
    }
}   
while (! reply.equalsIgnoreCase("no")) ;

if (count != 0) {
    System.out.println("The average of the numbers is " + 
            (total / count));
}

}

}

Alex
  • 83
  • 9
  • [Scanner is skipping nextLine() after using next() or nextFoo()?](//stackoverflow.com/a/13102066) – 001 Sep 29 '19 at 13:13
  • 1
    @JohnnyMopp Though that is related, the main problem is having is not related to that. It's more to do with trying to use `nextFloat` to read the string "no". – Sweeper Sep 29 '19 at 13:15
  • `nextFloat()` cannot store a String. Store the number to a String and cast it to a float. – Deane Kane Sep 29 '19 at 13:35
  • @Sweeper Ok, I see it now. OP has 2 issues. – 001 Sep 29 '19 at 13:37

1 Answers1

0
  • Remove first inputNumber = scan.nextFloat();
  • Fix loop.
  • Add scan.nextLine() after scan.nextFloat()
    public static void main(String[] args) {
        int count = 0;
        float total = 0f;
        float inputNumber = 0f;

        Scanner scan = new Scanner ( System.in );

        System.out.println("Want to enter a number?");
        String reply = scan.nextLine();

        while (reply.equalsIgnoreCase("yes")) {
            System.out.println("Enter a number > ");
            inputNumber = scan.nextFloat();
            scan.nextLine();
            total += inputNumber;
            count++;

            System.out.println("Enter another number, or enter \"no\" to terminate > ");
            reply = scan.nextLine();
        }

        if (count != 0) {
            System.out.println("The average of the numbers is " + (total / count));
        }
    }

EDIT

    public static void main(String[] args) {
        int count = 0;
        float total = 0f;
        float inputNumber = 0f;

        Scanner scan = new Scanner ( System.in );

        System.out.println("Want to enter a number?");
        String reply = scan.nextLine();

        if (!reply.equalsIgnoreCase("yes"))
            return;

        System.out.println("Enter a number > ");
        while (!reply.equalsIgnoreCase("no")) {
            reply = scan.nextLine();
            try {
                inputNumber = Float.parseFloat(reply);
            } catch (NumberFormatException e) {
                continue;
            }
            total += inputNumber;
            count++;
            System.out.println("Enter another number, or enter \"no\" to terminate > ");
        }
        if (count != 0) {
            System.out.println("The average of the numbers is " + (total / count));
        }
    }
shy4blue
  • 46
  • 5
  • If they enter a number you will exit the while loop at `reply = scan.nextLine()`. You would be better checking `!reply.eqaulsIsnoreCase("no))`. You then need to handle when reply is a number, cast it to a float, add it to the total and update the count. – Deane Kane Sep 29 '19 at 13:33
  • Thank you for the replies. I'm unfamiliar with try/catch. I've read up on it just now and I think I'm starting to see why it's useful here; is there a way to do this without try/catch, though? – Alex Sep 29 '19 at 17:21
  • You could use a regular expression to verify your input. https://stackoverflow.com/questions/14206768/how-to-check-if-a-string-is-numeric – shy4blue Sep 30 '19 at 01:09