user12169596
user12169596

Reputation:

How should I structure this while loop? (JAVA)

New to Java. How should I structure this while loop to re-enter the loop when user input is 'Y'? Should the while go at the very beginning? The sample of the output is below code.

import java.util.Scanner;

public class RamosSLE33 {


public static void main(String[] args) { 


char cont = 'Y';
String anniversaryGift = " ";
int year = 0;
Scanner input = new Scanner(System.in);

System.out.printf("ANNIVERSARY YEAR%n%n1. 50%n2. 55%n3. 60%n4. None of the above."
                  + "%n%nSelect the anniversary year: ");
year = input.nextInt();

if (year == 1) 
  System.out.printf("The anniversary gift is gold.");
  if (year == 2) 
    System.out.printf("The anniversary gift is emerald.");
    if (year == 3) 
      System.out.printf("The anniversary gift is diamond.");
      if (year == 4) 
        System.out.printf("Go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");

        cont = 'N';

while(Character.toUpperCase(cont) == 'Y') {
       System.out.printf("%nSearch for another anniversary gift? Enter 'Y' or 'N': ");
       cont = input.nextLine().charAt(0);
} // End while == Y    

} //End main()

} //End class RamosSLE33

SAMPLE OUTPUT

Upvotes: 0

Views: 94

Answers (2)

Lahiru Wijesekara
Lahiru Wijesekara

Reputation: 662

You have few mistakes in the program.

  1. Your while loop doesn't repetitively run the entire program

  2. The Scanner input might be a Resource leak hence you have not closed it.

Please refer to the corrected program as below

public class RamosSLE33 {

    public static void main(String[] args) {

        char cont = 'Y';
        int year = 0;
        Scanner input = new Scanner(System.in);

        while (Character.toUpperCase(cont) == 'Y') {
            System.out.printf("ANNIVERSARY YEAR%n%n1. 50%n2. 55%n3. 60%n4. None of the above."
                    + "%n%nSelect the anniversary year: ");
            year = input.nextInt();

            if (year == 1) {
                System.out.printf("The anniversary gift is gold.");
            } else if (year == 2) {
                System.out.printf("The anniversary gift is emerald.");
            } else if (year == 3) {
                System.out.printf("The anniversary gift is diamond.");
            } else if (year == 4) {
                System.out.printf("Go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");
            } else {
                System.out.printf("An invalid Input Number");
            }
            cont = 'N';

            System.out.printf("%nSearch for another anniversary gift? Enter 'Y' or 'N': ");
            cont = input.next(".").charAt(0);
        } // End while == Y
        input.close();
        System.out.printf("%n The progrm ends: ");
    } // End main()

}

Upvotes: 1

squishy
squishy

Reputation: 61

Not sure if you shortcutted copying the code, but the shorthand ifs only do one line you'd need braces{} for 2. this part

  if (year == 4) 
    System.out.printf("Go to www.bernardine.com/jewelry-anniv.htm#traditional for more gift choices.");

    cont = 'N';

Hence cont always equals 'N' before that loop, so it's never entered. That's probably what's getting you, rest is just me doing light code critiquing really.

You may also consider 'Y'.equals(cont.touppercase()) to avoid nullpointer exceptions when cont is null, though I think your Character factory might dodge that it's a good habit to do constants first.

maybe also using a do while instead so it always enters the thing to grab the input.nextline() before evaluating if it's Y and deciding to continue.

If you want it to reenter the whole method at the end look into recursion. You may need to make the cont variable class level or break it out into another method with cont as an argument; remember to have an exit condition if you go that route.

Upvotes: 0

Related Questions