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

Thread: Datatype comparisons in do while loops

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Datatype comparisons in do while loops

    Hi, I'm having an awkward time dealing with the comparison of user input:

    package randomExperiments;
     
    import java.util.Scanner;
     
    public class RandomExperiments {
     
    	public static void main(String[] args){
     
    		String password = "Megaman X";
    		Scanner str = new Scanner(System.in);
    		do{
    			System.out.println("We're still trapped in the do. Guess the password to get out!");
    			//String password = "Megaman X";
    			//Scanner str = new Scanner(System.in);
    			String strGuess = str.next();
    		}while(!strGuess.equals(password));
    		str.close();
     
    		int magicNumber = 40;
    		Scanner num = new Scanner(System.in);
    		do{
    			System.out.println("We're still trapped in the do. Guess the magic number to get out!");
    			int intGuess = num.nextInt();
    		}while(intGuess != magicNumber);
    		num.close();
    	}
    }

    Line 22 says, "strGuess cannot be resolved".

    Line 30 says, "intGuess cannot be resolved to a variable".

    What's wrong?
    Last edited by SamJava_the_Hut; December 6th, 2018 at 02:43 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Datatype comparisons in do while loops

    What's wrong?
    The variable is not defined in the scope where it is being referenced. Define it outside of the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Datatype comparisons in do while loops

    Why do you continue to 1) use multiple Scanner instances of System.in and 2) close them? And here's a freebie. You're going to have problems with your password. I suggest you use a print statement to print out the user's input to see what happens.

    Regards,
    Jim

  4. #4
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Datatype comparisons in do while loops

    Quote Originally Posted by jim829 View Post
    Why do you continue to 1) use multiple Scanner instances of System.in and 2) close them?
    Sorry Jim. I'm having a hard time wrapping my head around only being able to use one scanner in an entire class. It puzzles me that Java would have such a crippling limitation. In my inexperienced understanding, it seems like it would be common necessity to get user input more than once (and consequently, for more than one variable).

    Quote Originally Posted by Norm View Post
    The variable is not defined in the scope where it is being referenced. Define it outside of the loop.
    I suspected it was a scope problem, and your reply confirms that. But, at the same time, wouldn't defining strGuess outside of the loop defeat the whole purpose not letting the user out of the do while loop until he/she enters a string that matches the password?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Datatype comparisons in do while loops

    defining strGuess outside of the loop
    You can define a variable in one place with an initial value and assign it other values in other places.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Datatype comparisons in do while loops

    Quote Originally Posted by SamJava_the_Hut View Post
    Sorry Jim. I'm having a hard time wrapping my head around only being able to use one scanner in an entire class. It puzzles me that Java would have such a crippling limitation. In my inexperienced understanding, it seems like it would be common necessity to get user input more than once (and consequently, for more than one variable).
    You are missing the point. It is not a crippling limitation. First you may do it if you want. It is just inefficient and unnecessary. You can take as many inputs from a single scanner as you need to. Here is an example:
    import java.util.Scanner;
     
    public class ScannerDemo {
     
       public static void main(String[] args) {
    	  // get rid of annoying (and inappropriate) resource not closed message from compiler
    	  @SuppressWarnings("resource")
     
    	  Scanner input = new Scanner(System.in);
    	  System.out.print("Please enter your first name: ");
    	  String name = input.next();
     
              System.out.print("Please enter your age:");
    	  int age = input.nextInt();
     
    	  input.nextLine(); // get rid of lingering new line from previous input
     
    	  System.out.print("Please list three strings or ints on one line:");
    	  String line = input.nextLine();
     
    	  System.out.println("Your name is " + name);
    	  System.out.println("Your age is " + age + " years.");
    	  System.out.println("You chose to list\n" + line);
       }
    }

    As you learn more about Scanner you will find that in some situations there is a lingering new line in the buffer. So you need to read it and discard before carrying on. I recommend you carefully read the Scanner API documentation. It is a commonly used class and will be of great benefit to you.

    Regards,
    Jim

  7. #7
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Datatype comparisons in do while loops

    Quote Originally Posted by jim829 View Post
    You can take as many inputs from a single scanner as you need to.
    OK, now I think I understand. However, in the context of what I'm trying to do, I'm still having variable scope issues:

    package randomExperiments;
     
    import java.util.Scanner;
     
    public class RandomExperiments {
     
    	public static void main(String[] args){		
     
     
    		String password = "Megaman X";
    		Scanner input = new Scanner(System.in);		
    		do{
    			System.out.println("We're still trapped in the do. Guess the password to get out!");
    			String pw = input.nextLine();
    		}while(!pw.equals(password));//while(!input.equals(password));
     
    		int magicNumber = 40;		
    		do{
    			System.out.println("We're still trapped in the do. Guess the magic number to get out!");
    			int intGuess = input.nextInt();
    		}while(!input.equals(magicNumber));
     
    		String test = "password";
    		while(!input.equals(test)){
    			System.out.println("Your guess still doesn't match. Try again.");
    			String guess = input.nextLine();
    		}
    	}
    }

    On line 16:

    If I use
    while(!pw.equals(password))
    , pw cannot be resolved.

    If I use
    while(!input.equals(password))
    while(!input.equals(password)), pw from line 15, and input from line 12 are seen as never used.

    intGuess on line 21 and guess on line 27 are also looked at by the program as unused.

    Is there any way around this?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Datatype comparisons in do while loops

    pw cannot be resolved.
    Define pw before the loop. Assign it a value inside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Datatype comparisons in do while loops

    Just as a heads up, I have a slightly different program on a new hard drive.

    I now have a problem with line 29. Here's the code:
    package sam;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
     
    public class Test {
     
    	public static void main(String[] args){
     
    		String password = "Megaman X";
    		String pw = "";
    		Scanner input = new Scanner(System.in);		
    		do{
    			System.out.println("We're still trapped in the do. Guess the password to get out!");
    			pw = input.nextLine();
    			if(pw.equals(password)){
    				System.out.println("The password has been guessed. Exiting this do while loop.");
    			}
    		}while(!pw.equals(password));//while(!input.equals(password));
    		System.out.println("We're out of the do while loop for password " + password + ".");
     
     
    		int magicNumber = 40;
    		int intGuess;
    		do{
    			System.out.println("We're still trapped in the do. Guess the magic number to get out!");
    			intGuess = input.nextInt();
    		}while(!input.equals(magicNumber));//(input != magicNumber);
     
    		String test = "password";
    		while(!input.equals(test)){
    			System.out.println("Your guess still doesn't match. Try again.");
    			String guess = input.nextLine();
    		}		
     
    	}//end of main
    }//end of class Test

    If I use
    while(!input.equals(magicNumber));
    The error is "The method equals(Object) in the type Object is not applicable for the arguments (int)".

    If I use
    while(input != magicNumber);
    The error is "Incompatible operand types Scanner and int".

    How do I fix this while statement?
    Last edited by SamJava_the_Hut; December 31st, 2018 at 02:40 AM. Reason: I got the string comparison do while loop working. I now need help on the int comparison do while loop instead.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Datatype comparisons in do while loops

    How do I fix this while statement?
    I assume you want to compare the value entered by the user with the value of magicNumber.
    That value is in the variable: intGuess. Use that in the while statement

    The input variable refers to an instance of the Scanner class. Use it with Scanner class methods to get user input. For example:
    	pw = input.nextLine();
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Datatype comparisons in do while loops

    Based on everything that I've learned from this thread, i have come to a working solution:

    package randomExperiments;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class RandomExperiments {
     
    	public static void main(String[] args){		
     
    		String strGuess = "";
    		int intGuess;
     
    		String password = "Megaman X";
    		Scanner input = new Scanner(System.in);		
    		do{
    			System.out.println("We're still trapped in the do. Guess the password to get out!");
    			strGuess = input.nextLine();
    		}while(!strGuess.equals(password));		
    		System.out.println("We have guessed correctly for strGuess.");
     
    		System.out.println("We will now start guessing for intGuess in the next do while loop.");
    		int magicNumber = 40;		
    		do{
    			System.out.println("We're still trapped in the do. Guess the magic number to get out!");
    			intGuess = input.nextInt();
    		}while(intGuess != magicNumber);
    		System.out.println("We have guessed correctly for intGuess.");
     
    	}
    }

    I want to thank jim and norm to start incrementing my thanks counter, but I don't know how. So, thanks for your help in this thread

Similar Threads

  1. Comparing Strings
    By vysero in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 8th, 2014, 12:41 PM
  2. EASY [comparing two vectors of strings]
    By speaker in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2012, 06:19 PM
  3. [SOLVED] Comparing Date Strings
    By wdh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2012, 03:28 AM
  4. Comparing a character to an array of strings
    By Hashmeer169 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 07:55 PM
  5. Comparing Strings?
    By wandertheverse in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 10:32 PM