What I'm trying to do is format input from a text area so that it is all uppercase letters and numbers, eliminating all punctuatoin and whitespace. I want to use a method format() to loop through all the characters in the text and use a switch statement to determine whether the character is A-Z or 0-9. If it is, I want to append the character to the end of a new String variable. Something like this, with "text" and "newText" as global String variables:

public static void format() {


char letter = 0;


text = text.toUpperCase();


for( i = 0; i < text.length(); i++ ) {
letter = text.charAt( i ) ;


switch( letter ) {
case A:
newText = newText + letter;
break;
case B:
newText = newText + letter;
break;


//... and so on from A-Z and 0-9
}
}

The problem is that it won't allow me to use the data type "char" for a switch statement (it thinks A, B, C, and so on are variables), and I'm guessing you probably can't append a "char" to a string either. Is there a function I can use in place of "charAt()" to get the characters from "text" and give them the data type "String" instead of "char"? Also, if so, will "Sting" work in a switch statement? For instance:

// I want "letter" to be a string variable in this case


switch( letter ) {
case A:
newText = newText + letter;
break;
case B:
... and so on

And one last question, is there a better way to check the character than using the long switch statement?

Thanks in advance!
EvilObsidian :twisted:

Edited 3 Years Ago by Nick Evan: Fixed formatting

Your entire idea makes little sense.
You say you want to convert a String to uppercase and eliminate whitespace, then give an example where there's only a single uppercase letter.

String has a function to convert a String to uppercase built in, so no need for your messy ideas.
Whitespace elimination is only slightly more involved, involving another single function from class String.

Read the API docs to find out all you need to know, the entire procedure can be written as one line of code, 2 at most.

}
}

The problem is that it won't allow me to use the data type "char" for a switch statement

switch( letter ) {
case A:
newText = newText + letter;
break;
case B:
... and so on

And one last question, is there a better way to check the character than using the long switch statement?

Thanks in advance!
EvilObsidian :twisted:

switch statement does check char data types. You MUST denote them with single quotes. ex: case 'A' or case 'B'

Also, there really is no point in using the switch statement because you can simply use a loop for all of it.

you can loop through using char's just like you can loop through using ints

for (int i=0; i<text.length(); i++)
 {
   for(char c='A'; c<'Z'; c++)
  {
     if (text.charAt(i) == c)
    {
       //it's uppercase
    }
  }
 }

that should help out a little, but the best thing is do what jwenting said and read some of the methods in the API.

Sorry if my english was bad or not understandable, but I just woke up.

;)

This article has been dead for over six months. Start a new discussion instead.