jSeesFor3ver
jSeesFor3ver

Reputation: 83

If/else statement for certain case

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;
   static int blank = 3;

    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'

        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 == blank) {                                  //I think the problem
//          hanoi(height - 1, source, auxiliary, destination);//Lies with this
//          moves++;                                          //else
//          hanoi(height - 1, auxiliary, destination, source);//statement         
//       }   
    }
}

Upvotes: 0

Views: 83

Answers (1)

Rahul
Rahul

Reputation: 45060

You could do the check immediately after getting the input itself instead of checking it in the method.

String height = scanner.nextLine();
int iHeight = height.trim().isEmpty() ? 3 : Integer.parseInt(height);
// The above code snippet will read the input and set the iHeight value as 3 if no input is entered.

And remove the else part from the hanoi() method.

Edit: In case the option to show steps is not required, you need to add an if and show the option to display steps in it.

String height = scanner.nextLine();
int iHeight = 3; // Default is 3
if (!height.trim().isEmpty()) { // If not empty
    iHeight = Integer.parseInt(height); // Use that value

    // And display the option to show steps
    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");
}

Upvotes: 1

Related Questions