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 3 of 3

Thread: If/else statement working incorrectly...

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If/else statement working incorrectly...

    Hi this is a towers of hanoi puzzle program. Right now I am having trouble with getting the program to solve for 3 discs if when prompted 'Enter the number of discs' is blank (i.e. enter key is hit without inputting any integer value).

    My if/else statement inside my hanoi method is where I think the problem is. I commented out where I think the problem is. How do I get the program to just solve for just 3 discs if nothing is input when prompted for 'Enter the number of discs'?

    Code:

    import java.util.Scanner;
     
    public class TowerOfHanoi4 {
    	static int moves = 0;
    	static boolean displayMoves = false;
     
    	public static void main(String[] args) {
     
    		System.out.println("Enter the Number of Discs : ");
    		Scanner scanner = new Scanner(System.in);
    		int iHeight = scanner.nextInt();
    		char source = 'S', auxiliary = 'D', destination = 'A'; // name poles or
    																// 'Needles'
          // int blank = null;
     
    		System.out.println("Press 'v' or 'V' for a list of moves");
    		Scanner show = new Scanner(System.in);
    		String c = show.next();
    		displayMoves = c.equalsIgnoreCase("v");
     
     
    		hanoi(iHeight, source, destination, auxiliary);
    		System.out.println(" Total Moves : " + moves);
    	}
     
    	static void hanoi(int height, char source, char destination, char auxiliary) {
    		if (height >= 1) {
    			hanoi(height - 1, source, auxiliary, destination);
    			if (displayMoves) {
    				System.out.println(" Move disc from needle " + source + " to "
    						+ destination);
    			}
    			moves++;
    			hanoi(height - 1, auxiliary, destination, source);
    		}
    //       else (height = 3) {                                  //I think the problem
    //          hanoi(height - 1, source, auxiliary, destination);//Lies with this
    //          moves++;                                          //else
    //          hanoi(height - 1, auxiliary, destination, source);//statement         
    //       }   
    	}
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: If/else statement working incorrectly...

    else (height = 3)
    This is a syntax error for two reasons. It will not compile.

    The correct structure of an if /else if / else statement is:

    if (height >= 1) {
         // do something
     
    } else if (height == 3) {
         //  note that it is else if, 
         //  also note the double equals (==).  A single equals assigns a variable,  a double equals test for equality.
     
    }  else {
        // do something if the other two aren't true
     
    }

    But there is yet another problem here, an issue with the logic. If height >= 1 it will execute that branch of the if statement. Since 3 >= 1 it will never reach that part of the code.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If/else statement working incorrectly...

    cool thanks! got it working

Similar Threads

  1. if else if elese if.... statement not working
    By ahans in forum Loops & Control Statements
    Replies: 4
    Last Post: October 25th, 2013, 01:54 PM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  3. Using constructors incorrectly?
    By nikki101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 04:05 AM
  4. [SOLVED] if statement incorrectly comparing strings
    By Pantheon8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 14th, 2011, 08:59 PM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM

Tags for this Thread