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: Datatype comparisons in do while loops

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    6
    Thanks
    0
    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
    21,554
    Thanks
    52
    Thanked 2,317 Times in 2,273 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
    141
    My Mood
    Cool
    Thanks
    0
    Thanked 18 Times in 18 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
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    6
    Thanks
    0
    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
    21,554
    Thanks
    52
    Thanked 2,317 Times in 2,273 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
    141
    My Mood
    Cool
    Thanks
    0
    Thanked 18 Times in 18 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
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    6
    Thanks
    0
    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
    21,554
    Thanks
    52
    Thanked 2,317 Times in 2,273 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.

Similar Threads

  1. Comparing Strings
    By vysero in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 8th, 2014, 01: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, 07:19 PM
  3. [SOLVED] Comparing Date Strings
    By wdh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2012, 04: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