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: Need help getting things to loop correctly

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help getting things to loop correctly

    Alright i'm having trouble getting my loop to continue after one try (i am using a loopcounter, it just might not be in the right spot.) You're supposed to get 3 turns. I'm also having trouble getting this to work for every input except A>B. Any suggestions? Instructions are in the program.

    ]import java.util.Scanner;         // used for console input (from the keyboard)
     
    // Declare the class
    public class toothpickPuzzle
    {
        // Fields that can be accessed anywhere in the class go here
        Scanner keyboard = new Scanner( System.in);     // used to read user input
     
     
        public static void main(String[] args)  
        {
            // create an instance of this class
            toothpickPuzzle theSampleInstance = new toothpickPuzzle();
            // call a non-static method to do everything else
            theSampleInstance.mainLoop();     
     
            // Display identifying information
            System.out.println( "Author: Eric Grunauer\n" +
                                "Program: #2 Toothpick Puzzle\n" +
                                "T.A.: Rigel Gjomemo\n" +
                                "Date: September 10, 2010");
            System.out.println();
            System.out.println("Welcome to the Toothpick Puzzle.  Think of having 3 piles of toothpicks\n" +
                               "in front of you, where there are 24 toothpicks total:\n" +
                               " \n" +
                               "        Stack:    A    B    C\n" +
                               " Number of Toothpicks:    11    7    6\n" +
                               " \n" +
                               "The goal is to create 3 piles of 8 toothpicks in exactly 3 moves.  A move\n" +
                               "consists of moving toothpicks from one stack to a second stack, where the\n" +
                               "number of toothpicks moved is exactly the number that is in the destination\n" +
                               "stack.  In other words, to move from stack B (7 toothpicks) to stack C (6)\n" +
                               "as shown above, we would move 6 from B to C, leaving us with 1 in B and 12\n" +
                               "in stack C.");
            System.out.println();
            System.out.println();
            System.out.println("Here we go...");
            System.out.println();
            System.out.println();
     
            // declare variables used in the program  
            Scanner keyboard = new Scanner( System.in);
            // declare the variables to store the number of toothpicks in each stack 
            int stackA = 11; 
            int stackB = 7; 
            int stackC = 6; 
            int turnCounter = 0;
     
                // display original stack values
                System.out.println("           Stack:       A   B   C");
                System.out.printf("Number of Toothpicks:      %2d  %2d  %2d \n", stackA, stackB, stackC);
                System.out.println();
                System.out.print("Enter the stack from: "); 
                char sourceStack = ' '; //
     
                // Get the user input, convert it all to upper case, and retrieve the first character.
                // This first character should be 'A', 'B', or 'C', representing one of the stacks. 
                sourceStack = keyboard.next().toUpperCase().charAt(0);
     
                System.out.print("Enter the stack to: "); // repeat for the second stack
                char destinationStack = ' '; 
                // Get destination stack letter
                destinationStack = keyboard.next().toUpperCase().charAt(0);
     
                // Now use "if" statements to change the values stored in variables stackA, stackB, and stackC 
            while (turnCounter < 3)
            {
                if( sourceStack == 'A' && destinationStack == 'C') 
                { 
                    int numberToMove = stackC; // store how many should be moved 
                    stackA = stackA - numberToMove; // deduct from the source stack 
                    stackC = stackC + numberToMove; // move into the destination stack 
                } 
                if( sourceStack == 'A' && destinationStack == 'B') 
                { 
                    int numberToMove = stackB; // store how many should be moved 
                    stackA = stackA - numberToMove; // deduct from the source stack 
                    stackB = stackB + numberToMove; // move into the destination stack 
                } 
     
                if( sourceStack == 'B' && destinationStack == 'A') 
                { 
                    int numberToMove = stackA; // store how many should be moved 
                    stackB = stackB - numberToMove; // deduct from the source stack 
                    stackA = stackA + numberToMove; // move into the destination stack 
                }
                if( sourceStack == 'B' && destinationStack == 'C') 
                { 
                    int numberToMove = stackC; // store how many should be moved 
                    stackB = stackB - numberToMove; // deduct from the source stack 
                    stackC = stackC + numberToMove; // move into the destination stack 
                }
                if( sourceStack == 'C' && destinationStack == 'B') 
                { 
                    int numberToMove = stackB; // store how many should be moved 
                    stackC = stackC - numberToMove; // deduct from the source stack 
                    stackB = stackB + numberToMove; // move into the destination stack 
                } 
                if( sourceStack == 'C' && destinationStack == 'A') 
                { 
                    int numberToMove = stackA; // store how many should be moved 
                    stackC = stackC - numberToMove; // deduct from the source stack 
                    stackA = stackA + numberToMove; // move into the destination stack 
                } 
     
     
                System.out.println("Stacks A   B   C contain: ");
                System.out.printf("      %2d  %2d  %2d \n", stackA, stackB, stackC);
                turnCounter++;
            }
        }//end method mainLoop()
     
     
    }//end
    Last edited by egruna2; September 9th, 2010 at 11:26 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help getting things to loop correctly

    First question: Why use a while() rather than a for() loop?

    having trouble getting this to work for every input except A>B
    What does this mean?

    Try debugging your code by adding print outs of ALL the variables values as the loop executes.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help getting things to loop correctly

    where is the body of mainLoop?

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help getting things to loop correctly

    I'm confuzed because of this comment:

    }// end method mainLoop()

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help getting things to loop correctly

    Seems the OP missed a couple of lines when he posted his code. Add }\n void mainLoop() {
    after the call to mainLoop.

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help getting things to loop correctly

    This solves something I think:
    package GGG;
     
    import java.util.Scanner; // used for console input (from the keyboard)
     
    // Declare the class
    public class toothpickPuzzle {
    	// Fields that can be accessed anywhere in the class go here
    	Scanner keyboard = new Scanner(System.in); // used to read user input
     
    	public static void main(String[] args) {
    		// create an instance of this class
    		toothpickPuzzle theSampleInstance = new toothpickPuzzle();
    		// call a non-static method to do everything else
    		theSampleInstance.mainLoop();
    	}
     
    	void mainLoop() {
    		// Display identifying information
    		System.out.println("Author: Eric Grunauer\n"
    				+ "Program: #2 Toothpick Puzzle\n" 
    				+ "T.A.: Rigel Gjomemo\n"
    				+ "Date: September 10, 2010");
    		System.out.println();
    		System.out.println("Welcome to the Toothpick Puzzle.  Think of having 3 piles of toothpicks\n"
    						+ "in front of you, where there are 24 toothpicks total:\n"
    						+ " \n"
    						+ "                Stack:    A    B    C\n"
    						+ " Number of Toothpicks:    11    7    6\n"
    						+ " \n"
    						+ "The goal is to create 3 piles of 8 toothpicks in exactly 3 moves.  A move\n"
    						+ "consists of moving toothpicks from one stack to a second stack, where the\n"
    						+ "number of toothpicks moved is exactly the number that is in the destination\n"
    						+ "stack.  In other words, to move from stack B (7 toothpicks) to stack C (6)\n"
    						+ "as shown above, we would move 6 from B to C, leaving us with 1 in B and 12\n"
    						+ "in stack C.");
    		System.out.println();
    		System.out.println();
    		System.out.println("Here we go...");
    		System.out.println();
    		System.out.println();
     
    		// declare variables used in the program
    		Scanner keyboard = new Scanner(System.in);
    		// declare the variables to store the number of toothpicks in each stack
    		int stackA = 11;
    		int stackB = 7;
    		int stackC = 6;
    		int turnCounter = 0;
     
    		// display original stack values
    		System.out.println("           Stack:       A   B   C");
    		System.out.printf("Number of Toothpicks:      %2d  %2d  %2d \n",
    				stackA, stackB, stackC);
    		System.out.println();
    		System.out.print("Enter the stack from: ");
    		char sourceStack = ' '; //
     
    		// Get the user input, convert it all to upper case, and retrieve the
    		// first character.
    		// This first character should be 'A', 'B', or 'C', representing one of
    		// the stacks.
    		sourceStack = keyboard.next().toUpperCase().charAt(0);
    		System.out.print("Enter the stack to: "); // repeat for the second stack
    		char destinationStack = ' ';
    		// Get destination stack letter
    		destinationStack = keyboard.next().toUpperCase().charAt(0);
     
    		int tries=3;
    		// Now use "if" statements to change the values stored in variables
    		// stackA, stackB, and stackC
    		while (turnCounter < tries) {
     
    			if (sourceStack == 'A' && destinationStack == 'C') {
    				int numberToMove = stackC; // store how many should be moved
    				stackA = stackA - numberToMove; // deduct from the source stack
    				stackC = stackC + numberToMove; // move into the destination
    				// stack
    			}
    			if (sourceStack == 'A' && destinationStack == 'B') {
    				int numberToMove = stackB; // store how many should be moved
    				stackA = stackA - numberToMove; // deduct from the source stack
    				stackB = stackB + numberToMove; // move into the destination
    				// stack
    			}
     
    			if (sourceStack == 'B' && destinationStack == 'A') {
    				int numberToMove = stackA; // store how many should be moved
    				stackB = stackB - numberToMove; // deduct from the source stack
    				stackA = stackA + numberToMove; // move into the destination
    				// stack
    			}
    			if (sourceStack == 'B' && destinationStack == 'C') {
    				int numberToMove = stackC; // store how many should be moved
    				stackB = stackB - numberToMove; // deduct from the source stack
    				stackC = stackC + numberToMove; // move into the destination
    				// stack
    			}
    			if (sourceStack == 'C' && destinationStack == 'B') {
    				int numberToMove = stackB; // store how many should be moved
    				stackC = stackC - numberToMove; // deduct from the source stack
    				stackB = stackB + numberToMove; // move into the destination
    				// stack
    			}
    			if (sourceStack == 'C' && destinationStack == 'A') {
    				int numberToMove = stackA; // store how many should be moved
    				stackC = stackC - numberToMove; // deduct from the source stack
    				stackA = stackA + numberToMove; // move into the destination
    				// stack
    			}
    			turnCounter++;
    			System.out.println("Stacks A   B   C contain: ");
    			System.out.printf("      %2d  %2d  %2d \n", stackA, stackB, stackC);
     
    			if(turnCounter==tries){
    				continue;
    			}
     
    			System.out.print("Enter the stack from: ");
    			// Get the user input, convert it all to upper case, and retrieve the
    			// first character.
    			// This first character should be 'A', 'B', or 'C', representing one of
    			// the stacks.
    			sourceStack = keyboard.next().toUpperCase().charAt(0);
    			System.out.print("Enter the stack to: "); // repeat for the second stack
    			// Get destination stack letter
    			destinationStack = keyboard.next().toUpperCase().charAt(0);	
     
     
    		}
    	}// end method mainLoop()
     
    }// end

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help getting things to loop correctly

    Are you trying to solve the OPs problem or are you trying to help the OP solve the problem?

    If you're going to solve the problem at least explain what you did to find the problem and what you did to solve it.
    Giving code doesn't really let the OP go thru the learning process. Will you be there the next time the OP doesn't know how to solve a problem?

Similar Threads

  1. Displaying things in gui
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: March 29th, 2010, 12:21 PM
  2. XML, and other things.
    By Tortex in forum The Cafe
    Replies: 0
    Last Post: March 27th, 2010, 03:33 PM
  3. How would i do these things??
    By Curious in forum Java Theory & Questions
    Replies: 4
    Last Post: February 21st, 2010, 08:33 PM
  4. Code not printing correctly
    By Movies32 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2010, 03:59 PM
  5. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM