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

Thread: Trying to get user input to work correctly

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

    Default Trying to get user input to work correctly

    I have a towers of hanoi puzzle program that is getting closer to being done. My problem right now is trying to get my input from the user to work properly.

    If they type 'v' or 'V', then the steps to solve the puzzle will be displayed (Hence the output would be 'Move Disc from S to D' and so forth) . Else, if the user does not type 'v' or 'V', then the program proceeds with in solving the puzzle, displaying the Total moves but not displaying the steps.

    The problem I am having is the options aren't working like they are supposed to.

    Now the only thing wrong is when the user input's 'v' or 'V', the moves are not displayed correctly. Output:
    Enter the min number of discs : 
    2
    Press 'v' or 'V' for a list of moves
    v
     Move disc from needle S to A
     Total Moves : 3

    How can I accomplish having the moves displayed if the user types 'v' or 'V', and if the user types some other than this the output just displays the 'Total Moves'?

    Here is my code:

    import java.util.*;
     
    import java.util.Scanner;
     
    public class TowerOfHanoi4 {
       static int moves=0;
       public static void main(String[] args) {
     
       System.out.println("Enter the min number of discs : ");
            Scanner scanner = new Scanner(System.in);
            int iHtMn = scanner.nextInt();       //iHeightMin         
     
            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();
            // char lstep='v', lsteps='V';       // grab option v or V
     
       if (c.equalsIgnoreCase("v")){        //if option is not v or V, execute code and only display total moves
        hanoi(iHtMn, source, destination, auxiliary); //else, user typed v or V and moves are displayed
        System.out.println(" Move disc from needle "+source+" to "+destination);
        System.out.println(" Total Moves : "+moves);
       } else {
          hanoi(iHtMn, source, destination, auxiliary);
          System.out.println(" Total Moves : "+moves);
         }  
       }
     
     
       public static void hanoi(int htmn,char  source,char  destination,char  auxiliary)
          {
          if (htmn >=1)
              {
                 hanoi(htmn-1, source, auxiliary, destination); // move n-1 disks from source to auxilary
                  // System.out.println(" Move disc from needle "+source+" to "+destination); // move nth disk to destination
                 moves++;     
                 hanoi(htmn-1, auxiliary, destination, source);//move n-1 disks from auxiliary to Destination
              }
          }
    }
    Last edited by jcfor3ver; November 5th, 2013 at 10:33 PM. Reason: updated code, still not functioning properly though


  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: Trying to get user input to work correctly

    What problems are you having? Are you working on the design or on coding from the design?
    Do you have any specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Trying to get user input to work correctly

    How do I get the user option 'v' or 'V' to display all of the moves? And in the same question, if they do not type 'v' or 'V', how do I just execute the program without displaying the moves??

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trying to get user input to work correctly

    If you read user input as a String you can use the equalsIgnoreCase method.
    Improving the world one idiot at a time!

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

    Default Re: Trying to get user input to work correctly

    Updated code above!!! Only problem I am having now is displaying the moves when the option v or V is typed.

  6. #6
    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: Trying to get user input to work correctly

    Use a boolean variable to remember that the user has entered the v/V and test it to know if a move is to be displayed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Trying to get user input to work correctly

    just use another variable when you call hanoi function...
    import java.util.Scanner;
     
    public class TowerOfHanoi4 {
       static int moves=0;
       public static void main(String[] args) {
    	   System.out.println("Enter the min number of discs : ");
    	   Scanner scanner = new Scanner(System.in);
    	   int iHtMn = scanner.nextInt();		//iHeightMin
    	   char source='S', auxiliary='A', destination='D';		//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();
    		   hanoi(iHtMn, source, destination, auxiliary,c);
    		   System.out.println(" Total Moves : "+moves);
     
    	   show.close();
    	   scanner.close();
       }
       public static void hanoi(int htmn,char  source,char  destination,char  auxiliary, String c){
    	   if (htmn >=1){
    		   hanoi(htmn-1, source, auxiliary, destination,c);		// move n-1 disks from source to auxiliary
    		   if(c.equalsIgnoreCase("v"))
    		   System.out.println(" Move disc from needle "+source+" to "+destination); 		// move nth disk to destination
    		   moves++;     
    		   hanoi(htmn-1, auxiliary, destination, source,c);		//move n-1 disks from auxiliary to Destination
    		   }
    	   }
       }
    Last edited by aprabhat; November 7th, 2013 at 01:09 AM. Reason: forgot to add code tags

  8. #8
    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: Trying to get user input to work correctly

    @aprabhat
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program Works on NetBeans, but Jar file doesn't work correctly.
    By LeandroRodirgues in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 12:36 PM
  2. My text input file is not working correctly?
    By MLeclerc182 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2012, 08:34 PM
  3. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  4. JLabel and set Location doesn't work correctly
    By Fantasy in forum AWT / Java Swing
    Replies: 6
    Last Post: November 15th, 2011, 12:02 PM
  5. 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

Tags for this Thread