Solved

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

Posted on 2011-09-14
9
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

733 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