Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Capitalizing a letter - semantic problem

    Hello. As an exercise, I wrote a quickie program to convert hexidecimal characters to their base-10 values. The program works, with a minor exception. I wanted the program to properly handle both lower case and capital letters. The program outputs the correct values for capital letters, but gives me the default error term if I enter a lowercase letter.

    Can anyone help me fix this? Thanks in advance.

    Here is the code:

    Java Code:
    // This program converts a hexidecimal character into the base-10 value.
    // The program outputs a "-1" for characters that are not valid hexidecimal 
    // characters.
    
    public class Hexi {
    	
    	static int hexValue(char hex) {	// conversion method
    		int result = -1;
    		char ch = hex;
    		if (Character.isLetter(ch) ) {
    			Character.toUpperCase(ch);
    			switch(ch) {
    				case 'A':
    					result = 10;										break;
    				case 'B':
    					result = 11;
    					break;
    				case 'C':
    					result = 12;
    					break;
    				case 'D':
    					result = 13;
    					break;
    				case 'E':
    					result = 14;
    					break;
    				case 'F':
    					result = 15;
    					break;
    				default:
    					result = -1;
    					break;
    			}
    		}
    		else {
    			switch(ch) {
    				case '0':
    					result = 0;
    					break;
    				case '1':
    					result = 1;
    					break;
    				case '2':
    					result = 2;
    					break;
    				case '3':
    					result = 3;
    					break;
    				case '4':
    					result = 4;
    					break;
    				case '5':
    					result = 5;
    					break;
    				case '6':
    					result = 6;
    					break;
    				case '7':
    					result = 7;
    					break;
    				case '8':
    					result = 8;
    					break;
    				case '9':
    					result = 9;
    					break;
    			}
    		}		
    		return result;
    	}
    
    	public static void main(String[] args) {	// main method
    		
    		char hex;
    		int out;
    		System.out.print("Enter a character: ");
    		hex = TextIO.getChar();
    		out = hexValue(hex);
    		System.out.println("The hexidecimal value of your character is: " + out );
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Capitalizing a letter - semantic problem

    toUpperCase() returns the uppercase char. It cannot change the value of the supplied char.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Capitalizing a letter - semantic problem

    OK, that would explain my error!!

    My alternate plan is to do this:
    case 'a', 'A': result = 10; break;

    I think this alternate will work, but I was hoping for something a little more elegant.

    Thanks!!

    Quote Originally Posted by Tolls View Post
    toUpperCase() returns the uppercase char. It cannot change the value of the supplied char.

Similar Threads

  1. Semantic data structure
    By dacoolest in forum Advanced Java
    Replies: 6
    Last Post: 01-12-2012, 07:25 PM
  2. Letter counter homework problem
    By Djgnl in forum New To Java
    Replies: 15
    Last Post: 09-24-2011, 03:37 AM
  3. Probabilistic latent semantic analysis(PSLA)
    By ovanwijk in forum Advanced Java
    Replies: 2
    Last Post: 06-15-2011, 08:25 PM
  4. Replies: 4
    Last Post: 01-25-2011, 11:36 PM
  5. Semantic Error: Message not understood by class
    By JVassie in forum New To Java
    Replies: 5
    Last Post: 04-15-2010, 03:39 PM

Posting Permissions

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