Home > Net >  What am I missing when trying to loop my switch statement?
What am I missing when trying to loop my switch statement?

Time:10-30

I am unable to loop the end of my switch statement to the beginning based on User Input. I want to have a yes or no question at the end of the switch, separate from the default. Yes the program will continue as planned, if no i want the program to be able loop to the beginning of the switch where it ask the user what size dog they would like.

import java.util.*;

class Main {
  public static void main(String[] args) {
    AdoptionClient client = new AdoptionClient();
    client.setName("Johnny Depp");
    System.out.println(client.getName());

    int count = 0;
    do {
      System.out.println("Count is: "   count);
      count  ;

      System.out.println("\nWhat size dog do you want to get!:\n"   "1) small\n"   "2) medium\n"   "3) large\n"
            "and any other number for non dog options!");

      Scanner input = new Scanner(System.in);

      int choice = input.nextInt();
       while (count > 5) {
         /// selected text ///

      switch (choice) {
        case 1:
          System.out.println("We have one small breeded dog. A Chihuahua is up for adoption!");
          System.out.println();
          System.out.println("Here is more information!");
          Chihuahua goliath = new Chihuahua("Goliath", 4, "Chihuahua", "aggressive");
          goliath.printDogInfo();
          System.out.println("To take me home press 5, or 6 or higher for no!");
          break;

        case 2:
          System.out.println("We have one medium breeded dog. A Labrador is up for adoption!");
          System.out.println();
          System.out.println("Here is more information!");
          Labrador hendrix = new Labrador("Hendrix", 8, "Labrador", "not aggressive");
          hendrix.printDogInfo();
          break;

        case 3:
          System.out.println("We have one large breeded dog. A Great dane is up for adoption!!");
          System.out.println();
          System.out.println("Here is more information!");
          Greatdane toodles = new Greatdane("Toodles", 2, "Greatdane", " not aggressive");
          toodles.printDogInfo();
          break;

        default:
          System.out.println("We are only giving dogs up for adoption today!");
      }    while (count > 5) {
         /// selected text ///      
         System.out.println("5 to take me home, 6 or higher for no.");
           }
      }
    

    }
      
    
  }
}

I have tried a do-while and while loop, expecting the end to loop but the while loop being an infinite loop seemed to loop the program in the wrong spot and I was just getting an infinite loop of one case that resulted in error. With the while loop, I expected it to work as planned but again it would loop in the wrong place. I attempted to fix that problem but my program results in a parsing error or unable to start statement with }. I also did a try and catch, to see if that would improve loop but that didn't work as well.

CodePudding user response:

A couple of things:

  • What is the purpose of count? Is it for your own debugging purposes?
  • Your system infinitely loops because you have no way to break out of your loop
  • Why have a while loop inside of a do-while loop?
  • Note that you have not handled the cases if the user inputs 5 or 6.

Instead you should loop depending on user input:

do {
      System.out.println("\nWhat size dog do you want to get!:\n"   "1) small\n"   "2) medium\n"   "3) large\n"   "0) quit\n"
            "and any other number for non dog options!");

      Scanner input = new Scanner(System.in);

      int choice = input.nextInt();
      switch (choice) {
        case 1:
          System.out.println("We have one small breeded dog. A Chihuahua is up for adoption!");
          System.out.println();
          System.out.println("Here is more information!");
          Chihuahua goliath = new Chihuahua("Goliath", 4, "Chihuahua", "aggressive");
          goliath.printDogInfo();
          System.out.println("To take me home press 5, or 6 or higher for no!");
          break;

        case 2:
          System.out.println("We have one medium breeded dog. A Labrador is up for adoption!");
          System.out.println();
          System.out.println("Here is more information!");
          Labrador hendrix = new Labrador("Hendrix", 8, "Labrador", "not aggressive");
          hendrix.printDogInfo();
          System.out.println("To take me home press 5, or 6 or higher for no!");
          break;

        case 3:
          System.out.println("We have one large breeded dog. A Great dane is up for adoption!");
          System.out.println();
          System.out.println("Here is more information!");
          Greatdane toodles = new Greatdane("Toodles", 2, "Greatdane", "not aggressive");
          toodles.printDogInfo();
          System.out.println("To take me home press 5, or 6 or higher for no!");
          break;

        default:
          System.out.println("We are only giving dogs up for adoption today!");
} while (choice != 0);
System.out.println("Goodbye\n"); 
//Goodbye message so you know your program has ended. This is not necessary.  

Changes I've made:

  • Added a quit option so that the user can quit out of the program
  • Changed some grammar to be more consistent
  • Removed the counter & the loop inside the loop

CodePudding user response:

I think about repeating until the number 0 is pressed.

When 0 is pressed, the chosen dog will be shown.

Animal class

public class Animal {

    public Animal(String name, Integer age, String breed, String behavior) {
        this.name = name;
        this.age = age;
        this.breed = breed;
        this.behavior = behavior;
    }
    private String name;
    private Integer age;
    private String breed;
    private String behavior;

    public void printDogInfo() {
        System.out.printf("Name: "   name);
        System.out.printf("\nage: "   age);
        System.out.printf("\nbreed: "   breed);
        System.out.printf("\nbehavior: "   behavior);
    }
}

AdoptionClient class

public class AdoptionClient {
    private String name;
    private Animal animal;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Animal getAnimal() {
        return animal;
    }
    public void setAnimal(Animal animal) {
        this.animal = animal;
    }
}

Animal class

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        AdoptionClient client = new AdoptionClient();
        client.setName("Johnny Depp");
        System.out.println(client.getName());

        int count = 0;
        int exit;
        do {
            System.out.println("Count is: "   count);
            count  ;

            System.out.println("""
                    What size dog do you want to get!:
                    1) small
                    2) medium
                    3) large
                    and any other number for non dog options!""");

            Scanner input = new Scanner(System.in);

            int choice = input.nextInt();

            /// selected text ///
            Animal animal = null;
            switch (choice) {
                case 1 -> {
                    animal = new Animal("Goliath", 4, "Chihuahua", "aggressive");
                    System.out.println("We have one small breeded dog. A Chihuahua is up for adoption!");
                    System.out.println();
                    System.out.println("Here is more information!");
                    animal.printDogInfo();
                    System.out.println("To take me home press 5, or 6 or higher for no!");
                }
                case 2 -> {
                    animal = new Animal("Hendrix", 8, "Labrador", "not aggressive");
                    System.out.println("We have one medium breeded dog. A Labrador is up for adoption!");
                    System.out.println();
                    System.out.println("Here is more information!");
                    animal.printDogInfo();
                }
                case 3 -> {
                    System.out.println("We have one large breeded dog. A Great dane is up for adoption!!");
                    System.out.println();
                    System.out.println("Here is more information!");
                    animal = new Animal("Toodles", 2, "Greatdane", " not aggressive");
                    animal.printDogInfo();
                }
                default -> System.out.println("We are only giving dogs up for adoption today!");
            }

            System.out.println("5 to take me home, 6 or higher for no.");
            int confirm = input.nextInt();
            if (confirm == 5) {
                client.setAnimal(animal);   // Use array if necessary
            }

            System.out.println("0 exit, 1 or higher for no.");
            exit = input.nextInt();
        } while (exit > 0);

        if (client.getAnimal() == null) {
            System.out.println("No dog selected");
        } else {
            System.out.println("The selected dog is");
            client.getAnimal().printDogInfo();
        }
    }
}
  • Related