robbiedarza robbiedarza - 1 month ago 9
Java Question

Java For Loop data validation

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

Dan Dan
Answer

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