Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,029 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,956 people online right now. Registration is fast and FREE... Join Now!




Switching Upper to LowerCase and vice versa

 
Reply to this topicStart new topic

Switching Upper to LowerCase and vice versa

summer291
post 13 Aug, 2008 - 08:17 AM
Post #1


New D.I.C Head

*
Joined: 29 Apr, 2007
Posts: 8


My Contributions


Hello I'm trying to convert from Uppercase to lowercase like
Hello would be hELLO.

I'm not quite sure what I'm doing wrong when I enter in my sentence it doesn't swith the case or even display the sentence back to me.
CODE

import javax.swing.JOptionPane;


class UpperCase {

public static void main(String[] args){
  String name;
  int count, lengt;
   int converted =0;
  char letter;
  
  name = JOptionPane.showInputDialog(null, "Please Enter a Sentence.");
    


  count = name.length();

      for (int i = 0; i < name.length(); i++) {
      letter = name.charAt(i);
                      char ch = name.charAt(i);

                      if (Character.isUpperCase(ch))
                          ch = Character.toLowerCase(ch);
                      else if (Character.isLowerCase(ch))
                          ch = Character.toUpperCase(ch); {

                      converted++;
                  }
}
JOptionPane.showMessageDialog(null,  " Your sentence swithed converted " +  "" +
converted);
}
}


Thanks

This post has been edited by 1lacca: 29 Aug, 2008 - 12:16 AM
User is offlineProfile CardPM

Go to the top of the page

Locke37
post 13 Aug, 2008 - 09:17 AM
Post #2


I'm not a thief...I prefer the term TREASURE HUNTER!

Group Icon
Joined: 20 Mar, 2008
Posts: 910



Thanked 30 times

Dream Kudos: 300
My Contributions


In the future --> code.gif

Well, my first thought would be to use arrays and comaprison.

java
import javax.swing.JOptionPane;

public class UpperCase
{
public static void main(String[] args)
{
String sentence = "";
int count, length;
char ch;

char[] uppers = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z'};

char[] lowers = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'};

sentence = JOptionPane.showInputDialog("Enter a sentence:");

String newSentence = "";

boolean found = false;

for (int x = 0; x < sentence.length(); x++)
{
found = false;

ch = sentence.charAt(x);

for (int i = 0; i < uppers.length(); i++)
{
if (ch == uppers[i])
{
found = true;

String s = ch.toString();
s = s.toLowerCase();
newSentence += s;
break;
}
}

if (found)
continue;

for (int i = 0; i < lowers.length(); i++)
{
if (ch == lowers[i])
{
String s = ch.toString();
s = s.toUpperCase();
newSentence += s;
break;
}
}

JOptionPane.showMessageDialog(null, "Your sentence converted is...\n" + newSentence);
}
}
}


Hope this helps! smile.gif

This post has been edited by Locke37: 13 Aug, 2008 - 09:29 AM
User is offlineProfile CardPM

Go to the top of the page

dobbersp
post 13 Aug, 2008 - 10:00 AM
Post #3


New D.I.C Head

*
Joined: 10 Jun, 2008
Posts: 16



Thanked 1 times
My Contributions


also, it appears that you do not actually change the string.
you set a character to a position in the string, and change the character.

heres my attempt to show an example:

STRING

CHAR = T

switch CHAR (CHAR = t)

STRING CHAR = t

all you have done is changed the char, but not your string.

also, you have several redundant vars, such as "letter" and "ch"
and your "converted" variable does not control a loop. maybe you were using them as debug tools?

anyhow, I hope this helped in at least the slightest manner.
cheers.
User is offlineProfile CardPM

Go to the top of the page

thenovices
post 13 Aug, 2008 - 11:02 PM
Post #4


D.I.C Head

**
Joined: 18 Jan, 2008
Posts: 73



Thanked 7 times
My Contributions


umm, there are several useful static methods in the Character class that would help a lot in this type of problem. They are:

CODE
boolean isUpperCase(char c)
boolean isLowerCase(char c)
char toUpperCase(char c)
char toLowerCase(char c)


with these methods, solving this problem should be a piece of cake.

This post has been edited by thenovices: 13 Aug, 2008 - 11:02 PM
User is offlineProfile CardPM

Go to the top of the page

Locke37
post 14 Aug, 2008 - 10:55 AM
Post #5


I'm not a thief...I prefer the term TREASURE HUNTER!

Group Icon
Joined: 20 Mar, 2008
Posts: 910



Thanked 30 times

Dream Kudos: 300
My Contributions


QUOTE(thenovices @ 14 Aug, 2008 - 12:02 AM) *

umm, there are several useful static methods in the Character class that would help a lot in this type of problem. They are:

CODE
boolean isUpperCase(char c)
boolean isLowerCase(char c)
char toUpperCase(char c)
char toLowerCase(char c)


with these methods, solving this problem should be a piece of cake.


Hm...I learned something today...never knew these methods existed. Never really had a use for them before though, so I never had a reason to use them/know they existed. icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

alantliber
post 15 Aug, 2008 - 11:11 PM
Post #6


New D.I.C Head

*
Joined: 15 Aug, 2008
Posts: 4


My Contributions


Have a look at this.

Your code fixed with comments.

CODE

import javax.swing.JOptionPane;

//Warning - if you don't declare this as "public class UpperCase" (without quotes) it will have package scope.
class UpperCase //Is this the best and most descriptive name? Perhaps something like ChangeCase would be better.
{
   //It might be a good idea to use methods - if you're not sure how try googling "java methods", you'll find a whole host of resources.
   public static void main(String[] args)
   {
      StringBuffer temp = new StringBuffer(); //this is a mutable string class
      String name;
      //int count, lengt; //these variables aren't doing anything
      //int converted = 0;//Why do you need a count of how many characters it is looping through? You're not using it.
      //char letter; //this variable isn't doing anything
      char ch; //moved the declaration to here
      
      try //If the user presses cancel it bombs out with a null pointer exception. At the very least you should catch this. There are better ways of writing this, but this is quick and simple.
      {
         name = JOptionPane.showInputDialog(null, "Please enter a sentence.");
      }
      catch(NullPointerException npe)
      {
         System.exit(0); //exit the program if the user presses cancel
      }

      
      //count = name.length(); //this isn't doing anything
      
      for (int i = 0; i < name.length(); i++)
      {
         //letter = name.charAt(i); //redundant - ch is doing al the work
         ch = name.charAt(i); //moved the declaration from here
        
         if (Character.isUpperCase(ch))
            ch = Character.toLowerCase(ch);
         else if (Character.isLowerCase(ch))
            ch = Character.toUpperCase(ch);
         //{ //these braces are unneccesary
        
         //you aren't actually changing the string, you are just picking a character out of it and changing the character
         temp.append(ch);//this appends a character onto the StringBuffer
        
         //converted++; //you're not using this count anywhere
         //} //these braces are unneccesary
      }
      
      //JOptionPane.showMessageDialog(null, " Your sentence swithed converted " + "" + converted);//you're not outputing the string - you're outputting a count of how many characters are in the string
      JOptionPane.showMessageDialog(null, " Your sentence converted is " + temp.toString()); //try this - changed the output text to "Your sentence converted is", took away empty quotes and changed variable to temp.toString (which is how to extract a String from a StringBuffer)
   }
}



Your code fixed without comments.

CODE

import javax.swing.JOptionPane;

class ChangeCase
{
   public static void main(String[] args)
   {
      StringBuffer temp = new StringBuffer();
      String name;
      char ch;
      
      try
      {
         name = JOptionPane.showInputDialog(null, "Please enter a sentence.");
      }
      catch(NullPointerException npe)
      {
         System.exit(0);
      }
      
      for (int i = 0; i < name.length(); i++)
      {
         ch = name.charAt(i);
        
         if (Character.isUpperCase(ch))
            ch = Character.toLowerCase(ch);
         else if (Character.isLowerCase(ch))
            ch = Character.toUpperCase(ch);
        
         temp.append(ch);
      }
      
      JOptionPane.showMessageDialog(null, " Your sentence converted is " + temp.toString());
   }
}


Note: it's good practice to indent your code - it makes it a lot easier to read.

This is personal preference but I find it easier to see which braces belong to each other if I put the first brace on its own line. However many people also put it on the line following the loop/class/method etc.

Hope this helps.
Alan
User is offlineProfile CardPM

Go to the top of the page

lordms12
post 16 Aug, 2008 - 03:22 AM
Post #7


D.I.C Regular

Group Icon
Joined: 16 Feb, 2008
Posts: 312



Thanked 13 times

Dream Kudos: 225
My Contributions


name.toLowerCase()
User is offlineProfile CardPM

Go to the top of the page

alantliber
post 28 Aug, 2008 - 05:54 PM
Post #8


New D.I.C Head

*
Joined: 15 Aug, 2008
Posts: 4


My Contributions


QUOTE(lordms12 @ 16 Aug, 2008 - 04:22 AM) *

name.toLowerCase()


Yeah but if you read summer291's example they want to convert upper to lower AND lower to upper...

Alan
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 05:37AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month