Results 1 to 3 of 3

Thread: Need help with a loop

  1. #1
    FLAC
    Join Date
    Apr 2005
    Location
    Queens, New York
    Posts
    1,386

    Need help with a loop

    Ok so i am writing a little program for school. The program is 99% done all i want to do left is add a loop so it will keep going back to the "selection menu" It is coded in java. I was wondering because i cant get a loop to seem to work. WOuld anyone beable to help put a loop so it keeps going back the the menu after the selection? ALso i need to add an exit button well here is the program:

    Code:
    import java.io.*;
    
    public class PetTest {
        public static void main(String[] args) {
        	 try {
                BufferedReader br = new BufferedReader(
                                    new InputStreamReader(System.in));
    			System.out.println("------------------------------------------------");
                System.out.println("Please Choose From the Following:\n 1. Dogs " +
                                   "2. Cats 3. Fish 4. Birds  5. VIEW ALL ");
    			System.out.println("------------------------------------------------");
                String line = br.readLine().trim();
                int selection = Integer.parseInt(line);
                String s = "";
                switch(selection) {
                    case 1:
                        s = "dog";
                        break;
                    case 2:
                        s = "cat";
                        break;
                    case 3:
                        s = "bird";
                        break;
                    case 4:
                        s = "fish";
                        break;
                    case 5:
                        s = "View All";
                        break;
                    default:
                        s = "unexected selection";
                }
                System.out.println("selection = " + s);
                Dog dog = null ;
                Cat cat = null;
                Fish fish = null;
                Bird bird = null;
                
                if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All"))
                {
                dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY);
                System.out.println("\ndog = " + dog);
                }
                
                if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){
                cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED);
                System.out.println("\ncat = " + cat);}
                
                 if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){
                bird = new Bird(3, 2, 5.00, "Canary", "USA");
                System.out.println("\nbird = " + bird);}
                
                 if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){
                 fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT);
                System.out.println("\nfish = " + fish);
                }
                
                br.close();
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
     
            
            
        }
    }
     
    class Pet {
        int age;
        double weight;
        double price;
     
        protected Pet(int age, double weight, double price) {
            this.age = age;
            this.weight = weight;
            this.price = price;
        }
     
        public String toString() {
            //String name = getClass().getName();
            return ", \nage:" + age +
                   ", \nweight:" + weight +
                   ", \nprice:" + price;
        }
    }
     
    class Dog extends Pet {
        String breed;
        String foodType;
        final static String CANNED = "canned food";
        final static String DRY    = "dry food";
     
        public Dog(int age, double weight, double price,
                   String breed, String foodType) {
            super(age, weight, price);
            this.breed = breed;
            this.foodType = foodType;
        }
     
        public String toString() {
            return "\n\nDog \nbreed:" + breed +
                   ", \nfoodType:" + foodType + super.toString() + "";
        }
    }
     
    class Cat extends Pet {
        String breed;
        String clawStatus;
        final static String CLAWED   = "has claws";
        final static String DECLAWED = "de-clawed";
     
        public Cat(int age, double weight, double price,
                   String breed, String clawStatus) {
            super(age, weight, price);
            this.breed = breed;
            this.clawStatus = clawStatus;
        }
     
        public String toString() {
            return "\n\nCat \nbreed:" + breed +
                   ", \nclawStatus:" + clawStatus +
                    super.toString() + "";
        }
    }
     
    class Bird extends Pet {
        String type;
        String nationalOrigin;
     
        public Bird(int age, double weight, double price,
                    String type, String origin) {
            super(age, weight, price);
            this.type = type;
            nationalOrigin = origin;
        }
     
        public String toString() {
            return "\n\nBird \ntype:" + type +
                   ", \nnationalOrigin:" + nationalOrigin +
                    super.toString() + "";
        }
    }
     
    class Fish extends Pet {
        String type;
        String waterType;
        final static String FRESH = "freah water";
        final static String SALT  = "salt water";
     
        public Fish(int age, double weight, double price,
                    String type, String habitat) {
            super(age, weight, price);
            this.type = type;
            waterType = habitat;
        }
     
        public String toString() {
            return "\n\nFish \ntype:" + type +
                   ", \nwaterType:" + waterType +
                    super.toString() + "";
        }
    }
    2002 Mitsubishi Galant
    Progress: 90% [-▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓- ->

    Carputer Specs:
    Via M10K
    512mb Ram
    60GB HDD
    Souund Blaster Audigy2 NX
    OPUS ITX PC Case


  2. #2
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,049
    edit: nevermind, at first glance I thought polymorphic, but I see now it isnt... I hate polymorphic substitution.

    Ok well just put it in a while loop, with the break statement being a switch.


    Code:
    import java.io.*;
    
    public class PetTest {
        public static void main(String[] args) {
             while(true) {
        	 try {
                BufferedReader br = new BufferedReader(
                                    new InputStreamReader(System.in));
    			System.out.println("------------------------------------------------");
                System.out.println("Please Choose From the Following:\n 1. Dogs " +
                                   "2. Cats 3. Fish 4. Birds  5. VIEW ALL ");
    			System.out.println("------------------------------------------------");
                String line = br.readLine().trim();
                int selection = Integer.parseInt(line);
                String s = "";
                switch(selection) {
                    case 1:
                        s = "dog";
                        break;
                    case 2:
                        s = "cat";
                        break;
                    case 3:
                        s = "bird";
                        break;
                    case 4:
                        s = "fish";
                        break;
                    case 5:
                        s = "View All";
                        break;
                    case 6:
                        s = "EXIT";
                        break;
                    default:
                        s = "unexected selection";
                }
                System.out.println("selection = " + s);
                Dog dog = null ;
                Cat cat = null;
                Fish fish = null;
                Bird bird = null;
                if(s.equalsIgnoreCase("exit"))
                {
                      break;
                }
                if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All"))
                {
                dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY);
                System.out.println("\ndog = " + dog);
                }
                
                if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){
                cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED);
                System.out.println("\ncat = " + cat);}
                
                 if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){
                bird = new Bird(3, 2, 5.00, "Canary", "USA");
                System.out.println("\nbird = " + bird);}
                
                 if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){
                 fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT);
                System.out.println("\nfish = " + fish);
                }
                }
                br.close();
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
     
            
            
        }
    }
     
    class Pet {
        int age;
        double weight;
        double price;
     
        protected Pet(int age, double weight, double price) {
            this.age = age;
            this.weight = weight;
            this.price = price;
        }
     
        public String toString() {
            //String name = getClass().getName();
            return ", \nage:" + age +
                   ", \nweight:" + weight +
                   ", \nprice:" + price;
        }
    }
     
    class Dog extends Pet {
        String breed;
        String foodType;
        final static String CANNED = "canned food";
        final static String DRY    = "dry food";
     
        public Dog(int age, double weight, double price,
                   String breed, String foodType) {
            super(age, weight, price);
            this.breed = breed;
            this.foodType = foodType;
        }
     
        public String toString() {
            return "\n\nDog \nbreed:" + breed +
                   ", \nfoodType:" + foodType + super.toString() + "";
        }
    }
     
    class Cat extends Pet {
        String breed;
        String clawStatus;
        final static String CLAWED   = "has claws";
        final static String DECLAWED = "de-clawed";
     
        public Cat(int age, double weight, double price,
                   String breed, String clawStatus) {
            super(age, weight, price);
            this.breed = breed;
            this.clawStatus = clawStatus;
        }
     
        public String toString() {
            return "\n\nCat \nbreed:" + breed +
                   ", \nclawStatus:" + clawStatus +
                    super.toString() + "";
        }
    }
     
    class Bird extends Pet {
        String type;
        String nationalOrigin;
     
        public Bird(int age, double weight, double price,
                    String type, String origin) {
            super(age, weight, price);
            this.type = type;
            nationalOrigin = origin;
        }
     
        public String toString() {
            return "\n\nBird \ntype:" + type +
                   ", \nnationalOrigin:" + nationalOrigin +
                    super.toString() + "";
        }
    }
     
    class Fish extends Pet {
        String type;
        String waterType;
        final static String FRESH = "freah water";
        final static String SALT  = "salt water";
     
        public Fish(int age, double weight, double price,
                    String type, String habitat) {
            super(age, weight, price);
            this.type = type;
            waterType = habitat;
        }
     
        public String toString() {
            return "\n\nFish \ntype:" + type +
                   ", \nwaterType:" + waterType +
                    super.toString() + "";
        }
    }
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

  3. #3
    Super Moderator. If my typing sucks it's probably because I'm driving.... turbocad6's Avatar
    Join Date
    Oct 2004
    Location
    NY
    Posts
    6,234
    gotta love the badger

Similar Threads

  1. Strange ground loop problem.
    By norbie in forum Car Audio
    Replies: 5
    Last Post: 06-18-2006, 03:25 PM
  2. Re-Ground HU or Ground Loop Isolator?
    By Incinorator in forum Car Audio
    Replies: 5
    Last Post: 04-20-2006, 08:52 PM
  3. I tried everything, can't get rid of this ground loop
    By intense in forum General Hardware Discussion
    Replies: 3
    Last Post: 07-10-2005, 09:58 AM
  4. VIDEO groud loop isolator
    By lookinco in forum LCD/Display
    Replies: 8
    Last Post: 10-07-2004, 07:17 AM
  5. Turtle Beach Santa Cruz Sound Card GIVES me a ground loop?
    By rmjjensen in forum General Hardware Discussion
    Replies: 2
    Last Post: 11-15-2003, 08:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •