1

I am currently doing some java excersises in uni and ive been stuck on this one for about 5 hours now! I am practising For loops and have the loop ask 5 times for a number from 1 to 3. When testing, if I enter an invalid selection it carries on and includes the invalid selection as a zero, I have got an error message working when an invalid input is entered but it still carries on until the loop finishes, I know there is a way to return to the beggining of the selection but I cant figure it out. I have searched everywhere for a solution but cannot find it! I know it cant be much and I'm not back in uni for a few days so I cant ask the lecturer and I would really like to crack on to the next chapter.

Here is my code (I know its probably a bit scrappy!!), thanks, Rob

   import java.util.Scanner;

/* this is s a survey of how 5 people sweeten thier coffee */

class coffee
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        int person, preference, nothing, sugar, sweetner;

        String pluralone = "People dont";
        String pluraltwo = "People use";
        String pluralthree = "People use";

        person = 0;
        preference = 0;
        nothing = 0;
        sugar = 0;
        sweetner = 0;

        for (person = 1; person <= 5; person++)

        {
            System.out.println("How do you sweeten your coffee");
            System.out.println("1. I Don't");
            System.out.println("2. With Sweetner");
            System.out.println("3. With Sugar");

            preference = input.nextInt();

            if (preference != 1 && preference != 2 && preference != 3)
                System.out.println("Sorry that is not a valid option");

            else if (preference == 1)
                nothing++;

            else if (preference == 1)
                sweetner++;

            else
                sugar++;
        }

        System.out.println("Survey Report");

        System.out.println("#############");

        if (nothing < 2)

        {
            pluralone = "person doesnt";
        }

        System.out.println(nothing + "  " + " " + pluralone + " sweeten thier coffee");

        if (sweetner < 2)

        {
            pluraltwo = "person uses";
        }

        System.out.println(sweetner + "  " + pluraltwo + " " + "sweetner to sweeten thier coffee");

        if (sugar < 2)

        {
            pluralthree = "person uses";
        }

        System.out.println(sugar + "  " + pluralthree + " " + "sugar to sweeten thier coffee ");

    }
}

enter image description here

3
  • 2
    Please don't post text in screenshots. Post text as text. Commented Jan 20, 2016 at 10:44
  • could you include a sample input for your problem, the actual output and the expected output? Commented Jan 20, 2016 at 10:45
  • the question is to 'trap for out range values and make sure your program still processes 5 valid selections, thx for the help Commented Jan 20, 2016 at 11:03

4 Answers 4

3

just ask for the users selection in a while loop so that it doesn't continue until a valid option has been entered, something like:

preference = input.nextInt();
while (preference != 1 && preference != 2 && preference != 3) {
    System.out.println("Sorry that is not a valid option");
    preference = input.nextInt();
}

alternatively you could decrement person in your if statement to cause another iteration of the for loop, but that's a bit hacky:

if (preference != 1 && preference != 2 && preference != 3) {
    System.out.println("Sorry that is not a valid option");
    person--;
}
Sign up to request clarification or add additional context in comments.

1 Comment

glad I could help :)
0

If you change your for loop to this

for (person = 1; person <= 5; person ++)
{
    System.out.println ("How do you sweeten your coffee");
    System.out.println ("1. I Don't");
    System.out.println ("2. With Sweetner");
    System.out.println ("3. With Sugar");

    preference = input.nextInt();

    while(preference != 1 && preference != 2 && preference != 3) {
        System.out.println ("Sorry that is not a valid option");
        System.out.println ("How do you sweeten your coffee");
        System.out.println ("1. I Don't");
        System.out.println ("2. With Sweetner");
        System.out.println ("3. With Sugar");
        preference = input.nextInt();
    }

    if(preference == 1) {
        nothing ++;
    } else if(preference == 2) {
        sweetner ++;
    } else if(preference == 3) {
        sugar ++;
    }
}

This will fix it

1 Comment

Hello, I did the while loop and it works, thank you very much. While loops are the next part in the boo that im moving onto so its given me a head start too. Many thanks to you and everyone else who replied. Cheers, Robin
0

Replace the if/else with an Switch/Case:

preference = input.nextInt();
switch(preference) {
  case 1: 
    nothing++;
    break;
  case 2:
    sweetner++;
    break;
  case 3:
    sugar++;
    break;
  default:
    System.out.println("Sorry, thats not a valid option! Please pick a valid option");
    preference = input.nextInt();
    person--;
    break;

}

Comments

0

I did the while loop and it works, thank you very much Dan. While loops are the next part in the book that im moving onto so its given me a head start too. Many thanks to you and everyone else who replied, a couple of the other things suggested seem a little advanced for me but I know I can always refer to the comments if I need them in the future. Thanks again, Robin.

1 Comment

Hey just a quick note on this website please don't post a thank you as an answer. Also, if the question has been answered correctly please click the tick next to the answer which you feel was correct. This just lets others know the question has been answered and it also gives you and the author of the answer reputation points. Thanks Dan. P.S. I'm glad I could help

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.