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!
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.
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.
//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 }
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.