Hi everyone
I am having a lot of trouble trying to get the toUpperCase method working. I have basically put a String of text into a character array and I want to convert a certain character in a certain place in the String to uppercase.

Here is what I have:

for (int i = 0; i < Text.length(); i++) {

           if (character[i] == LetterFind)
           if (Character.isLowerCase(LetterFind))              

           Text = string.replace(LetterFind, LetterReplace);
           Text = string.toUpperCase(LetterFind);

There is a previous bit of code which specifies which characters in the String to do this to, shown here as the variable "LetterFind".

All I get is the error message: cannot find symbol - method toUpperCase(char)

Ignore the other line
Text = string.replace(LetterFind, LetterReplace);

That is a find and replace function I have been using.

Thanks =)

Recommended Answers

All 24 Replies

toUpperCase in the Char class is a static method, so the correct way to use it is:
charArray = Char.toUpperCase(charArray);
You seem to be getting into difficulties with String vs char. It may be bettert to convert your String to a char array, do the selective case changes in the char array, then create a new String from the updated array.

I am having a lot of trouble trying to get the toUpperCase method working. I have basically put a String of text into a character array and I want to convert a certain character in a certain place in the String to uppercase.

well i doubt that u can do that simultaneously . well if u want to find a particular character and then replace or replace at particular position .

1. input the string
string s = new string ("text");

2.convert the string to char array
char s1[] = new char[4];
s1 = s.toCharArray();

3. iterate using the character array and change the character u want to

4. covert the character to string
s = s1.toString();

then i guess u can re print the string.

You could also use the String's toUpperCase as long as you converted the char to a String first. Since charArray is a char, if you did charArray + "", that is now a String in Java. Pass that to the toUpperCase method and it will return an uppercase String version of that letter.

(James' solution is better than that, just telling you that it is possible to do so)

You could also use the ANSI table and add the appropriate amount to the character to convert it to upper case.


edit: I said ANSI, java uses unicode character set though, sorry

By changing the code to the following I get the error message "char cannot be dereferenced" How can I sort this? Thanks

{

     String Findinput = JOptionPane.showInputDialog("Please enter the letter you would like to change");
     char LetterFind = Findinput.charAt(0);
     String Replaceinput = JOptionPane.showInputDialog("Please enter the letter you would like to replace it with");
     char LetterReplace = Replaceinput.charAt(0);


       char[] characters = Text.toCharArray();


         for (int i = 0; i <Text.length(); i++) {

           if (characters[i] == LetterFind && characters[i].isLowerCase())

           Text = string.replace(LetterFind, LetterReplace);

           characters[i] = Char.toUpperCase(characters[i]);    


    }

char[] characters = Text.toCharArray();

wat is "text" here , i mean wat does it hold .
besides u could have been a little more explicit in mentioning about your code .

as far as i guess from this its a mismatch between string and char data type .
why dont u first try keeping all the data type as same preferably char and then solve it , ur job will be a lot more easier n then you can go ahead doing using string n char types .

characters[i].isLowerCase() is not valid for char - it's a primitive, not an object.

It's a static method for the Char class - ie
Char.isLowerCase(characters0;
I still say you should do all the replacing in the char array, then convert back to String when it's all finished. The String replace you are doing is at best redundant, and at worst will replace all occurrences of LetterFind when you just wanted one replaced.
ps unless LetterFind is a class it should start with a lower-case letter ie letterFind.

I have tried changing it to the following but now get the error message:

"non-static method replace (char,char) cannot be referenced from a static context"

I am really sorry if I sound stupid but I just have no idea how to fix this!

{

     String Findinput = JOptionPane.showInputDialog("Please enter the letter you would like to change");
     char letterFind = Findinput.charAt(0);
     String Replaceinput = JOptionPane.showInputDialog("Please enter the letter you would like to replace it with");
     char letterReplace = Replaceinput.charAt(0);


       char[] characters = Text.toCharArray();

         for (int i = 0; i <Text.length(); i++) {

           if (characters[i] == letterFind)  

           Text = String.replace(letterFind, letterReplace);

           string characterString = new String (characters);

           if (characterString[i].isLowerCase())

           characters[i] = Char.toUpperCase(characters[i]);                

    }

Sorry man, I've given you the best advice I can, twice, but you don't want to follow it, so good luck, I'm outta here.

It's a static method for the Char class - ie
Char.isLowerCase(characters);

Yep, but he was calling it on an element in an array of 'char', hence the "char cannot be dereferenced" error. If he used it as you indicated, a static method call, it would work fine.

But I don't understand what you mean! I have only been learning Java for a few months.

What is a static method call?

Thanks

well if ur a neophyte into this then the oly thing i recommend is ti do the program from basic
1. using char array . later on you can re do the same thing with utmost sophistication . sometimes simplicity is the best thing .

Thanks very much. So from what I understand this toUpperCase method only works with Strings and I am trying to use it with a char...

No. The String class and the Character class both have toUpperCase methods. If you try to use the String class's toUpperCase method with a Character, it will not work. However, if you use the Character class's toUpperCase method, it will work. You need to understand that the Character class's toUpperCase method is a static method. Therefore, the way that you call the method is Classname.methodName(arguments). In this case, that means Character.toUpperCase(yourCharacterHere). I hope you will read the article I linked you to about static methods.

Note: If you read the documentation for the toUpperCase method, you will notice that it takes an argument of type 'char', which is a primitive type (such as int, double, etc).

Success! I got rid of the error messages but now I get no character change in the character array?

   {

         String Findinput = JOptionPane.showInputDialog("Please enter the letter you would like to change");
         char letterFind = Findinput.charAt(0);
         String Replaceinput = JOptionPane.showInputDialog("Please enter the letter you would like to replace it with");
         char letterReplace = Replaceinput.charAt(0);


           char[] characters = Text.toCharArray();

            String str=Text; 

             for (int i = 0; i <Text.length(); i++) {

               if (characters[i] == letterFind && Character.isLowerCase(letterFind)) 

               characters[i] = letterReplace;

               characters[i] = Character.toUpperCase(letterReplace);

               Text = new String(characters);



        }

Thanks

Try to identify the area of your problem. You can do this by using print statements to verify that your major pieces of code are working correctly. For example, does your code ever get inside of the if statement? Then tell us where your areas of difficulty lie.

Also, post all relevant code, and post that code in code tags, read the sticky if you don't know how to do this. . I read your code but I don't see any errors.[CODE=Java]. I read your code but I don't see any errors.

It's really strange. I have been putting print statements in and outside of the loop. I also put one just after the if statement and it printed twice as I expected it to. When running properly though it just seems to be replacing all of the letters you want to change with an uppercase version, for example if you replaced 'G' with 'S' it would print 'SSSSSSSSSSSSS' for the number of times it loops.

while (selection==1)
         
         {
        
         String Findinput = JOptionPane.showInputDialog("Please enter the letter you would like to change");
         char letterFind = Findinput.charAt(0);
         String Replaceinput = JOptionPane.showInputDialog("Please enter the letter you would like to replace it with");
         char letterReplace = Replaceinput.charAt(0);
         
            
           char[] characters = Text.toCharArray();
           
         
            String str=Text; 
            
             for (int i = 0; i < Text.length(); i++) {
           
               if (characters[i] == letterFind && Character.isLowerCase(letterFind)) 
              
               characters[i] = letterReplace;

               characters[i] = Character.toUpperCase(letterReplace);
               
               Text = new String(characters); 
         }
     
         System.out.println (Text);

Have you looked at whether you might need some braces after your if() statement? I don't think you want to execute this characters[i] = Character.toUpperCase(letterReplace); for every iteration of your loop.

This is making me go crazy!!!

I am slowly progressing and have just spent the last hour and a half adding print line methods to try and work out what is going on here! I am really not sure what I am missing. Looking at it logically the first if statement is reached, if the letter is lower case and matches the character entered then the letter is replaced which now happens.

However it seems to then convert all the characters in the String to capitals instead of just the character which is specified in the code!?

I even added an extra if statement to try and fix the problem.

Maybe I am missing something really obvious, but I can't see it :(

Thanks guys

while (selection==1)
         
         {
        
         String Findinput = JOptionPane.showInputDialog("Please enter the letter you would like to change");
         char letterFind = Findinput.charAt(0);
         String Replaceinput = JOptionPane.showInputDialog("Please enter the letter you would like to replace it with");
         char letterReplace = Replaceinput.charAt(0);
         
            
           char[] characters = Text.toCharArray();
           
           
           
            String str=Text; 
            
             for (int i = 0; i < Text.length(); i++) {
           
               if (characters[i] == letterFind && Character.isLowerCase(letterFind)) 
               
               {
               characters[i] =letterReplace;
               }
               
              
             
               if (Character.isLowerCase(characters[i]))
               
               {
               characters[i] = Character.toUpperCase(characters[i]); 
               }  
               
               
            } 
               
               
               
               Text = new String(characters); 
               
               
          
          System.out.println (Text);

That's because this code

 if (Character.isLowerCase(characters[i]))

               {
               characters[i] = Character.toUpperCase(characters[i]); 
               }  

Converts all of the lower case characters into upper case ones. If you don't want to do that, then remove that code.

But how would I convert just the one character that is matched to uppercase?
Thanks

Thank you very much for everyones help! I have managed to get this working!! :)

Thank you very much for everyones help! I have managed to get this working!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.