Solved

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

Posted on 2011-09-14
9
257 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 80
expectj telnet failing 5 45
Chrome and Firefox Java 5 49
web project error add remove 1 27
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

791 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