SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,761
    Mentioned
    12 Post(s)
    Tagged
    0 Thread(s)

    Why is this not being updated?

    Code java:
    import java.util.Scanner;
    public class combinationLock
    {
    	//Sets the combination lock to B H W
    	public char FIRST_LETTER='B';
    	public char SECOND_LETTER='H';
    	public char THIRD_LETTER='W';
    	//Declare the 3 inputs of the user
    	public char first_input;
    	public char second_input;
    	public char third_input;
    	//Declare the 3 booleans to keep the loop working
    	public static boolean first;
    	public static boolean second;
    	public static boolean third;
     
    	public static void main(String args[])
    	{
    		//Declares the user interface and the constructor
    		combinationLock lock=new combinationLock();
    		Scanner numbers=new Scanner(System.in);
     
    		while(first==false || second==false || third==false)
    		{
    			//Start getting their first letter
    			System.out.println("Please enter in the first letter in the combination: ");
    			char first_input=numbers.next().charAt(0);
    			first_input=Character.toUpperCase(first_input);
     
    			//Start getting their second letter
    			System.out.println("Please enter in the second letter in the combination: ");
    			char second_input=numbers.next().charAt(0);
    			second_input=Character.toUpperCase(second_input);
     
    			//Start getting their third letter
    			System.out.println("Please enter in the third letter in the combination: ");
    			char third_input=numbers.next().charAt(0);
    			third_input=Character.toUpperCase(third_input);
     
    			//Begin running the methods to begin lock comparison
    			lock.lockComparison1(first_input, first);
    			lock.lockComparison2(second_input, second);
    			lock.lockComparison3(third_input, third);
    		}
    	}
     
    	//Start the first method for the lock combination of the first input
    	public boolean lockComparison1(char first_input, boolean first)
    	{
    		if(first_input==FIRST_LETTER)
    		{
    			first=true;
    			System.out.println("Your first guess, \"B\" is correct.");
    		}
    		else if(first_input==SECOND_LETTER || first_input==THIRD_LETTER)
    		{
    			System.out.println("Your first guess, \""+first_input+"\" was not the first letter in the combination, yet it is used in the actual combination");
    			first=false;
    		}
    		else if(first_input!=SECOND_LETTER || first_input!=THIRD_LETTER)
    		{
    			first=false;
    			System.out.println("You entered \""+first_input+"\" but it isn't a letter in any part of this combination.");
    		}
    		return first;
    	}
     
    	//Start the first method for the lock combination of the second input
    	public boolean lockComparison2(char second_input, boolean second)
    	{
    		if(second_input=='H')
    		{
    			second=true;
    			System.out.println("Your second guess, \"H\" is correct.");
    		}
    		else if(second_input==FIRST_LETTER || second_input==THIRD_LETTER)
    		{
    			System.out.println("Your second guess, \""+second_input+"\" was not the second letter in the combination, yet it is used in the actual combination");
    			second=false;
    		}
    		else if(second_input!=FIRST_LETTER || second_input!=THIRD_LETTER)
    		{
    			second=false;
    			System.out.println("You entered \""+second_input+"\" but it isn't a letter in any part of this combination.");
    		}
    		return second;
    	}
     
    	//Start the first method for the lock combination of the third input
    	public boolean lockComparison3(char third_input, boolean third)
    	{
    		if(third_input=='W')
    		{
    			third=true;
    			System.out.println("Your third guess, \"W\" is correct.");
    		}
    		else if(third_input==FIRST_LETTER || third_input==SECOND_LETTER)
    		{
    			System.out.println("Your third guess, \""+third_input+"\" was not the third letter in the combination, yet it is used in the actual combination");
    			third=false;
    		}
    		else if(third_input!=SECOND_LETTER || third_input!=FIRST_LETTER)
    		{
    			third=false;
    			System.out.println("You entered \""+third_input+"\" but it isn't a letter in any part of this combination.");
    		}
    		return third;
    	}
     
    }

    first second and third booleans need to be updated to true in order for the while loop to stop. In order for that to happen the user must input B H W as the 3 inputs but the scope of the variable (maybe?) isn't global enough to update. I guess the return statement in each function isn't being updated so I'm confused. What can I do to make these booleans get updated?

    Edit: Updated code.
    Edit2: Updated comments.
    Last edited by RyanReese; Oct 24, 2008 at 08:27. Reason: Updated comments
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    The keyword 'static' does two things
    first: it makes the variable or method available without the need to create an instance of the class. eg: Integer.MAX_VALUE

    second: it makes the value of the variable 'unmutable' or unchangeable. Even if you had assigned a value correctly (which we'll get into in a minute) it wouldn't work, it, actually, wouldn't even compile.

    In my reply to your other post, I brought up 'variable scope'. This refers to the section of code that can 'see' a variable.

    Your code 'public static boolean first;' declares a 'globally' available variable, but, you 'overrode' that variable by declaring a local variable with the same name: ' public boolean lockComparison1(char first_input, boolean first)'

    Compile and run the following:
    Code:
    public class Scope {
      public static void main( String[] args ) {
        new Scope();
      }
      
      public Scope() {
        int n = 5;
        changeValue( n );
        System.out.println( n );
        n = changeValue( n );
        System.out.println( n );
      }
      
      public int changeValue( int n ) {
        n = 0;
        return n;
      }
    }
    The output will be 5, then 0. Why?

    The n in the constructor is not the same n that is in the method changeValue. This is because only the value of n has been passed to the method, not a pointer or reference. Even if we change the method declaration to public int changeValue( int not_n ), the result will be the same.

    Let's change it a bit to get closer to your code
    Code:
    public class Scope {
    
      int n = 5;
    
      public static void main( String[] args ) {
        new Scope();
      }
      
      public Scope() {
        changeValue( n );
        System.out.println( n );
        n = changeValue( n );
        System.out.println( n );
      }
      
      public int changeValue( int n ) {
        n = 0;
        return n;
      }
    }
    I've moved the initial declaration of n to the class level, making it 'global', yet the result is the same. Again, this is because the global n is not the same as the n declared in the method declaration.

    One more change

    Code:
    public class Scope {
    
      int n = 5;
    
      public static void main( String[] args ) {
        new Scope();
      }
      
      public Scope() {
        changeValue();
        System.out.println( n );
        n = changeValue();
        System.out.println( n );
      }
      
      public int changeValue() {
        n = 0;
        return n;
      }
    }
    Now we get the result we're expecting, but the code is confusing. "Where did this magic 'n' come from and what is it for?" This is the question you will ask yourself when the code starts getting into 100s of lines. Declare what you need, when you need it.

  3. #3
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,761
    Mentioned
    12 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I got it working. I'm familiar with variable scopes from PHP. I added static to those because it gave me an error or something...I forget.

    Code:
    public static void main(String args[])
    	{
    		//Declares the user interface and the constructor
    		combinationLock lock=new combinationLock();
    		Scanner numbers=new Scanner(System.in);
    		
    		while(!first || !second || !third)
    		{
    			//Start getting their first letter
    			System.out.println("Please enter in the first letter in the combination: ");
    			char first_input=numbers.next().charAt(0);
    			first_input=Character.toUpperCase(first_input);
    		
    			//Start getting their second letter
    			System.out.println("Please enter in the second letter in the combination: ");
    			char second_input=numbers.next().charAt(0);
    			second_input=Character.toUpperCase(second_input);
    		
    			//Start getting their third letter
    			System.out.println("Please enter in the third letter in the combination: ");
    			char third_input=numbers.next().charAt(0);
    			third_input=Character.toUpperCase(third_input);
    			if(third_input==THIRD_LETTER && second_input==SECOND_LETTER && first_input==FIRST_LETTER)
    			{
    				first=true;
    				second=true;
    				third=true;
    				System.out.println("Congratulations, you opened the lock!");
    			}
    			else
    			{
    				//Begin running the methods to begin lock comparison
    				lock.lockComparison1(first_input, first);
    				lock.lockComparison2(second_input, second);
    				lock.lockComparison3(third_input, third);
    			}
    		}
    	}
    Made my main method into that and got it working.

    Thanks.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •