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

Thread: Repeating program issue

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Repeating program issue

    I need to get a method in my CommonUse class to work properly with any other class. What I'm aiming for is to use this particular method in future programs for school without having to rewrite the code every time.

    The purpose is to return a boolean as to whether or not to repeat a loop in another class.

    CommonUse class:
    	// Asks if user wants to repeat something. Parameter (output) is defined by programmer when calling
    	// this method based on the wording they want to use for the method. Example: "Would you like to
    	// retry?" or "Would you like to play again?"
    	public static boolean doAgain(String output) {
    		boolean repeat = true;
    		char response;
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println(output);
    		System.out.println("Y or y = Yes\nN or n = No");
    		response = keyboard.nextLine().charAt(0);
     
    		if ((response == 'Y') || (response == 'y'))
    			repeat = true;
    		else if ((response == 'N') || (response == 'n'))
    			repeat = false;
    		else {
    			System.out.println("Incorrect character entered. Please try again.\n");
    			doAgain(output);
    		}
    		return repeat;
    	}

    Test class:
    public static void main(String[] args) {
    		do {
    		        System.out.println("Test line\n");
    		} while (CommonUse.doAgain("Would you like to repeat this program?"));
    	}

    I can get the program to successfully repeat or exit with correct user input. However, if I enter a character other than a 'Y', 'y', 'N', or 'n', the doAgain() method does loop on itself again and seems to work, especially if I choose to repeat the program another time after the invalid attempt. But, if after putting, for example, a 'X', it gives the error message as usual, then repeats and asks for the correct input like I want it to. But if I enter 'N' or 'n', it repeats the program one more time, before I have to enter 'N' or 'n' again to finally exit.

    It seems like there is a residual value of true for the doAgain() method when it returns to the Test class, thus making it repeat again even though the input should have made it false.

    Any ideas? Thanks!


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Repeating program issue

    this should work right now (I ADDED SOME COMMENTS IN YOUR CLASS WHEN I CHANGED IT)


      	public static boolean doAgain(String output) {
    		boolean repeat = true;
    		String response;  //I CHANGED THE DATA TYPE INTO STRING
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println(output);
    		System.out.println("Y or y = Yes\nN or n = No");
    		response = keyboard.nextLine();
     
                    // I used equalsIgnoreCase() method of String class to compare Strings
    		if ((response.equalsIgnoreCase("Y")) || (response.equalsIgnoreCase("YES")))  
                              repeat = true;                                                
     
                    // I used equalsIgnoreCase() method of String class to compare Strings
    		else if ((response.equalsIgnoreCase("N")) || (response.equalsIgnoreCase("NO"))) 
    			repeat = false;                                                        
    		else {
    			System.out.println("Incorrect character entered. Please try again.\n");
    			doAgain(output);
    		}
    		return repeat;


    your main program..

     
            public static void main(String[] args) {
    		do {
    		        System.out.println("Test line\n");
    		} while (CommonUse.doAgain("Would you like to repeat this program?"));
    	}

    As far as I know about strings in java. i learned that "NEVER USE EQUALITY OPERATORS TO COMPARE STRINGS" "==" or "!=" ,use the methods of String class....

    1.) equals() method to compare strings
    2.) equalsIgnoreCase() to compare, no matter what is the case, (wheter capital or not)



    I change your input variable as String because char cannot be dereferenced
    but I hope the will be someone who can explain to us further more why characters cannot be dereferenced

    and Ill ask this in advance...


    is there any method that can compare characters..?? same methods like equals()? or equalsIgnoreCase()?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Repeating program issue

    characters are a primitive data type, and as such can be compared using the ==, !=, or any other valid comparison operator.

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Repeating program issue

    Yeah, I had originally use a String variable, but to reduce the amount of possible outcomes, I changed it to a char so only the first character of the text input will be read, and it allows me to use a call by value when comparing with "==" or "!=" rather than having a lot of errors if I used those operators in a call by reference in the case of a String. So the method works, but it's that final outcome where a boolean value of true seems to be stored that trips me up.

    Thanks though!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Repeating program issue

    In the case of case of an incorrect character (not y Y n N), your code is recursively calling doAgain, and in the end not returning the result of the next doAgain call. Try something like this:
    else {
    	System.out.println("Incorrect character entered. Please try again.\n");
    	return doAgain(output);
    }

  6. The Following 2 Users Say Thank You to copeg For This Useful Post:

    Bill_H (October 24th, 2009), Json (October 24th, 2009)

  7. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: Repeating program issue

    Quote Originally Posted by copeg View Post
    In the case of case of an incorrect character (not y Y n N), your code is recursively calling doAgain, and in the end not returning the result of the next doAgain call. Try something like this:
    else {
    	System.out.println("Incorrect character entered. Please try again.\n");
    	return doAgain(output);
    }
    That did it! Thanks a lot! We haven't covered recursion yet, though I've skimmed the chapter, so now I know what to do in the case of returning and repeating a method. Thanks again!

Similar Threads

  1. Issue with JTextField Locking
    By PekinSOFT.Systems in forum AWT / Java Swing
    Replies: 0
    Last Post: October 1st, 2009, 11:12 AM
  2. Replies: 3
    Last Post: May 18th, 2009, 03:45 AM
  3. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM