SitePoint Sponsor

User Tag List

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

    Why isn't this going to uppercase

    I have a project for my AP computer science class (12th grade. This is HW)

    I got moved up to independent study because I pulled ahead of the class and I have to do this assignment. I have to let the user input 3 letters uppercase or lowercase and then compare it to my combination and then give hints. Blah blah blah.

    I have this code

    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;
    	public static void main(String args[])
    	{
    		//Declares the user interface and the constructor
    		combinationLock lock=new combinationLock();
    		Scanner numbers=new Scanner(System.in);
     
    		//Start getting their first letter
    		System.out.println("Please enter in the first letter in the combination: ");
    		char first_input=numbers.next().charAt(0);
    		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);
    		Character.toUpperCase(second_input);
     
    		//Start getting their second letter
    		System.out.println("Please enter in the third letter in the combination: ");
    		char third_input=numbers.next().charAt(0);
    		Character.toUpperCase(third_input);
     
    		//Begin running the methods to begin lock comparison
    		lock.lockComparison1(first_input);
    		lock.lockComparison2(second_input);
    		lock.lockComparison3(third_input);
     
    	}
     
    	//Start the methods for the lock combination testing
    	public char 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)
    		{
    			System.out.println("You entered \""+first_input+"\" but it isn't a letter in any part of this combination.");
    		}
    		return first_input;
    	}
    	public char 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)
    		{
    			System.out.println("You entered \""+second_input+"\" but it isn't a letter in any part of this combination.");
    		}
    		return second_input;
    	}
    	public char 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)
    		{
    			System.out.println("You entered \""+third_input+"\" but it isn't a letter in any part of this combination.");
    		}
    		return third_input;
    	}
     
    }

    If you look in the main method it isn't converting it to uppercase which is what I need: the teacher will try and break my programs.

    Any idea why it isn't converting my chars?

    PS: No errors compiling or running. It is a logic error.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  2. #2
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,746
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Got it, I wasn't updating the variable so I just did first_input=...blah blah when converting it to uppercase and got it working.
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  3. #3
    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)
    I know this is a homework assignment, and that you're 'only' in high school, but I'm going to pick this apart anyway in an attempt to help you improve your coding.

    The format will be:
    your code
    my comment

    public class combinationLock
    Java class names always start with a capital letter

    public char FIRST_LETTER='B'; etc
    Variable names with all caps are considered constants and should be static.

    public char first_input; etc
    'Global' variables are frowned upon, declare what you need, when you need it.

    char first_input=numbers.next().charAt(0);
    You already declared first_input, etc, as global variables, but this still works - an excellent opportunity to see 'variable scopes' in action, but a poor programming practice.
    Code:
            //Start getting their first letter
            System.out.println("Please enter in the first letter in the combination: ");
            char first_input=numbers.next().charAt(0);
            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);
            Character.toUpperCase(second_input);
           
            //Start getting their second letter
            System.out.println("Please enter in the third letter in the combination: ");
            char third_input=numbers.next().charAt(0);
            Character.toUpperCase(third_input);
    These 'segments' do exactly the same thing, they should be made into one method that takes arguments to accomplish all three things.
    eg: public char getInput( String inputNumber ) //inputNumber eg: "first"

    public char lockComparison1(char first_input), etc
    Again, three nearly identical code blocks that need a little logic and an argument or two to accomplish the goal in one easy method.

  4. #4
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,746
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    We have only been coding java since September 1st...so yea...

    I thought all capital on variable names meant that they are static and can't be changed (as in the case with this)? Please respond to my otherthread since I have an actual problem there (your advice is DEFINATELY taken into account!!)
    Always looking for web design/development work.
    http://www.CodeFundamentals.com

  5. #5
    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 actual name of the variable is irrelevant, the use of the keyword 'static' is what tells the compiler to make a variable static.

  6. #6
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,746
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I know.
    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
  •