Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Hey guys, my While loop isn't working correctly.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hey guys, my While loop isn't working correctly.

    Okay, I attached the program specifications. Basically, I need to make a "salesman" program selling rocket ships. My problem is that the while loop isn't working correctly.

    It runs, but when it runs a 2nd time, it executes the line that asks what the name of the ship is and the line asking for how many shields. I also don't know how to make the loop end when the user responds with "no".

    Basically, the loop just doesn't work correctly, and I need help. I've run into a brick wall with this assignment.

    Here's the code for the main class:
    package lab05;
     
    import java.util.Scanner;
    import java.util.Random;
    import java.text.DecimalFormat;
     
    public class Lab05 
    {
     
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner(System.in);
            Random PRNG = new Random();
            DecimalFormat formatter = new DecimalFormat("#0.00");
            Spaceship ship1 = new Spaceship();
     
            System.out.print("Would you like to buy a ship? ");
            String ans = keyboard.nextLine();
     
            while(ans.equals("Yes")||ans.equals("yes")||ans.equals("Y")||ans.equals("y")) {
            System.out.print("What is the name of the ship? ");
            String tempName = keyboard.nextLine();
            ship1.setName(tempName);
     
            System.out.print("How many shields? ");
            int tempShields = keyboard.nextInt();
            ship1.setNumShields(tempShields);
     
     
            System.out.print("How many guns? ");
            int tempGuns = keyboard.nextInt();
            ship1.setNumGuns(tempGuns);
     
     
            System.out.print("How many engines? ");
            int tempEngines = keyboard.nextInt();
            ship1.setNumEngines(tempEngines);
     
            int rand = PRNG.nextInt(10)+1;
            switch (rand) {
                case 1:
                    ship1.setNumDroids(1);
                    break;
                case 2:
                    ship1.setNumDroids(1);
                    break;
                case 3:
                    ship1.setNumDroids(1);
                    break;
                case 4:
                    ship1.setNumDroids(2);
                    break;
                case 5:
                    ship1.setNumDroids(2);
                    break;
                case 6:
                    ship1.setNumDroids(0);
                    break;
                case 7:
                    ship1.setNumDroids(0);
                    break;
                case 8:
                    ship1.setNumDroids(0);
                    break;
                case 9:
                    ship1.setNumDroids(0);
                    break;
                case 10:
                    ship1.setNumDroids(0);
                    break;
                default:
                    ship1.setNumDroids(0);
                    break;
            }
     
     
            ship1.displayName();
            ship1.displayNumShields();
            ship1.displayNumGuns();
            ship1.displayNumEngines();
            ship1.displayNumDroids();
     
            }
        }
    }

    And here's the Spaceship class:
    package lab05;
     
    public class Spaceship 
    {
        public String nameShip;
        private int numShields;
        private int numGuns;
        private int numEngines;
        private int numDroids;
     
     
        public void setName(String name) {
            this.nameShip = name;
        }
     
        public String getName() {
            return nameShip;
        }
        public void displayName() {
            System.out.println("The name of the ship is " + getName());
        }
     
     
        public void setNumShields(int numShields) {
            this.numShields = numShields;
        }
        public int getNumShields() {
            return numShields;
        }
        public void displayNumShields() {
            System.out.println("The ship has " + getNumShields() + " shield(s)");
        }
     
     
        public void setNumGuns(int numGuns) {
            this.numGuns = numGuns;
        }
     
        public int getNumGuns() {
            return numGuns;
        }
        public void displayNumGuns() {
            System.out.println("The ship has " + getNumGuns() + " gun(s)");
        }
     
     
        public void setNumEngines(int numEngines) {
            this.numEngines = numEngines;
        }
     
        public int getNumEngines() {
            return numEngines;
        }
        public void displayNumEngines() {
            System.out.println("The ship has " + getNumEngines() + " engine(s)");
        }
     
     
        public void setNumDroids(int numDroids) {
            this.numDroids = numDroids;
        }
        public int getNumDroids() {
            return numDroids;
        }
        public void displayNumDroids() {
            if(numDroids > 0) {
            System.out.println("We're giving you " + getNumDroids() + " free droid(s)");
            }
            else {
            System.out.print("");
            }
        }
     
     
    }
    Attached Files Attached Files


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Hey guys, my While loop isn't working correctly.

    Why don't you use a while(true) condition for your while loop, ask the user if they want to buy another ship, if they do then continue to loop and if they don't then return false. I'm not able to go through the actual code through the moment, so I can't say anything else about your other problem at the moment

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys, my While loop isn't working correctly.

    Quote Originally Posted by Actinistia View Post
    Why don't you use a while(true) condition for your while loop, ask the user if they want to buy another ship, if they do then continue to loop and if they don't then return false. I'm not able to go through the actual code through the moment, so I can't say anything else about your other problem at the moment
    I'm not actually sure how I would do that. I need the user to input "Yes", "yes", "Y", or "y" to continue, so how would I make that into a true/false condition?

    Or could I throw an If statement at the end of the loop asking if they want another, and if no, then break the loop? I don't know what the most efficient way of doing this would be.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Hey guys, my While loop isn't working correctly.

    Yes, I'm pretty sure the second option would work. I'm pretty new to java, to be honest, but I think that the if statement would do the job. You can pretty much make the if statement the same as your while loop condition (i.e. making the user input "No, no, N, n") and tell the program to break if they input it.

    *side note*
    Since it's a string, I was wondering if it'd be easier if you'd use the string.toLowercase() or string.toUppercase() to make it just 2 inputs. But since the way you have it works, I'd just keep it as it is.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys, my While loop isn't working correctly.

    Quote Originally Posted by Actinistia View Post
    Yes, I'm pretty sure the second option would work. I'm pretty new to java, to be honest, but I think that the if statement would do the job. You can pretty much make the if statement the same as your while loop condition (i.e. making the user input "No, no, N, n") and tell the program to break if they input it.

    *side note*
    Since it's a string, I was wondering if it'd be easier if you'd use the string.toLowercase() or string.toUppercase() to make it just 2 inputs. But since the way you have it works, I'd just keep it as it is.
    Using the string.toLowercase() thing is a good idea, I'll do that.

    And my loop is still broken. Guess I'll completely rework the main class.

    If anybody has anything else to contribute, I'll gladly take hints.

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Hey guys, my While loop isn't working correctly.

    It is not good practice to use infinite while loops. Use a control variable!
    String cont = "yes"; //Control variable
    Scanner someScanner = new Scanner(System.in); //Scanner object
    while(cont.toLowerCase().startsWith("y"))
    {
      /*
       * All your code
       */
      System.out.println("Should we run this loop again?"); //You should probably reword that, I just scanned your code
      cont = someScanner.nextLine();
    }

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hey guys, my While loop isn't working correctly.

    Quote Originally Posted by Tjstretch View Post
    It is not good practice to use infinite while loops. Use a control variable!
    String cont = "yes"; //Control variable
    Scanner someScanner = new Scanner(System.in); //Scanner object
    while(cont.toLowerCase().startsWith("y"))
    {
      /*
       * All your code
       */
      System.out.println("Should we run this loop again?"); //You should probably reword that, I just scanned your code
      cont = someScanner.nextLine();
    }
    Well, that completely broke my program. It executes "Would you like to buy a ship?" and "What is the name of the ship?" at the same time, not taking any input for "Would you like to buy a ship?". It used to only do that when the loop ran the 2nd time, and now it does it automatically. :[

Similar Threads

  1. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 04:52 PM
  2. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 10:13 PM
  3. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM
  4. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  5. Need help getting things to loop correctly
    By egruna2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 9th, 2010, 05:53 PM