Solved

How do you convert a string from toLowerCase to toUpperCase and toLowerCase to toUpperCase?

Posted on 2011-09-14
9
254 Views
Last Modified: 2012-08-14
I have no clue how to figure this problem out.

public class Strings
{
    // instance variables - replace the example below with your own
   

    /**
     * Constructor for objects of class Strings
     */
    public Strings()
    {
       
    }

    /**
     * Write a method that takes a string and returns a new string that changes the case of every character in
     * the original string changed from lowercase to uppercase and vice versa. Note that upper/lower case doesn't
     * apply to some characters such as numbers or punctuation.
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public String changeCase(String s)
    {
        if(s.toLowerCase())
        {
            return s.toUpperCase();
            else if(s.toUpperCase())
            return s.toLowerCase();
        }
       
    }
}
0
Comment
Question by:ryanbecker24
  • 5
  • 4
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36540172
Is this an academic assignemnt?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36540175

well in any case I'm not writng anything for you but if you go to internet, you can find such things, like in this link:
http://stackoverflow.com/questions/1729778/how-can-i-invert-the-case-of-a-string-in-java
0
 

Author Comment

by:ryanbecker24
ID: 36540176
No, I have always wondered how to figure this out. I just decided to make an empty class just for fun. I know how to go from a regular string to lower case and reg string to upper case but I don't know how to go back and forth.
0
 

Author Comment

by:ryanbecker24
ID: 36540211
Thank you, I have been looking all over the internet for this. I copied and pasted it. It compiles but it doesn't show the case switch from uppercase to lowercase. It shows lowercase to uppercase. I am using BlueJ. Do you know why this is?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 47

Expert Comment

by:for_yan
ID: 36540229
Post your exact code, I'll look at it.
0
 

Author Comment

by:ryanbecker24
ID: 36540252
public String caseChange(String s)
   {     char[] characters = s.toCharArray();    
       for (int index = 0; index < characters.length; index++)    
       {         char c = characters[index];        
           if (Character.isUpperCase(c))        
           {            
               characters[index] = Character.toLowerCase(c);        
            }         else if (Character.isLowerCase(c))        
            {             characters[index] = Character.toUpperCase(c);        
            }    
        }     return new String(characters);
    }
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36540261
Just pasted your code - seesm to be working fine for me - look at the output below

 public static String caseChange(String s)
   {     char[] characters = s.toCharArray();
       for (int index = 0; index < characters.length; index++)
       {         char c = characters[index];
           if (Character.isUpperCase(c))
           {
               characters[index] = Character.toLowerCase(c);
            }         else if (Character.isLowerCase(c))
            {             characters[index] = Character.toUpperCase(c);
            }
        }     return new String(characters);
    }


    

    public static void main(String[] args) {


      System.out.println(caseChange("ASADhjRTklui54"));


}

Open in new window



output:

asadHJrtKLUI54

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 36540279
It works now. I just closed BlueJ and opened it again. Thanks.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36540284
You are welcome.

Better go away from BlueJ - that's not the most popular stuff - and perhpas has some quirks.
Rather use some normal IDE, like Eclipse.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now