The Artima Developer Community
Sponsored Link

Java Answers Forum
Converet Char to UpperCase

6 replies on 1 page. Most recent reply: May 12, 2003 2:16 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Converet Char to UpperCase Posted: May 2, 2003 12:46 AM
Reply to this message Reply
Advertisement
How do convert a char to uppercase?
Whenever I try to I get an error
char cannot be dereferenced what does this mean?


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Converet Char to UpperCase Posted: May 2, 2003 3:22 AM
Reply to this message Reply
It would be easier to explain the error if the code was present.

Currently, you can use this method. There may be better ways of conversion though.

Store the 'char' in a 'short' or 'byte' (if you are sure its going to be an alphabet):

byte b = 'a';

To convert to Uppercase, subtract 32 from its present ascii value and cast it back to 'char':

(char)b-32;

This should help.

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Converet Char to UpperCase Posted: May 2, 2003 6:30 AM
Reply to this message Reply
To convert a character to uppercase use the following code

char lowercase = 'x';
char uppercase = Character.toUpperCase( lowercase );

This converts the character argument to uppercase using case mapping information from the UnicodeData file.

Adam

Kevin

Posts: 27
Nickname: quasi
Registered: Apr, 2003

Re: Converet Char to UpperCase Posted: May 3, 2003 5:08 AM
Reply to this message Reply
Thanks Adam your code worked

Arbab-ul-hassan

Posts: 4
Nickname: umpirepk
Registered: Dec, 2002

Re: Converet Char to UpperCase Posted: May 9, 2003 4:21 AM
Reply to this message Reply
This Is The Code:
public class ATest{
public static void main(String args[]){

int b='a';
int c=b-32;
char d=(char)c;

System.out.println(d);
}
}

Neil

Posts: 5
Nickname: no158
Registered: May, 2003

Re: Converet Char to UpperCase Posted: May 10, 2003 12:09 PM
Reply to this message Reply
when you did that -32 to the char 'b' is that going to run the char through the ASCI code to the capital letters? If yes does that work with all letters in the alphabet

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Converet Char to UpperCase Posted: May 12, 2003 2:16 AM
Reply to this message Reply
The -32 solution (for want of a better name) will work for the English alphabet - possibly other alphabets too. However, it is better to use the

Character.toUpperCase( myCharacter );


method since this will take account for all alphabets that exist in Unicode and it means you don't have to worry about localisation problems with your code. In short, let Java worry about the details.

Adam

Flat View: This topic has 6 replies on 1 page
Topic: sorting in collection alphabetically problem! Previous Topic   Next Topic Topic: help!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use