# Thread: Capitalizing a letter - semantic problem

1. Member
Join Date
Mar 2011
Posts
93
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Capitalizing a letter - semantic problem

toUpperCase() returns the uppercase char. It cannot change the value of the supplied char.

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

## 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!!

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

#### Posting Permissions

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