Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Uppercase and Lowercase in a string

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Uppercase and Lowercase in a string

    Hi, my name is shane. The program is about determining if a character is upper or lower case and reversing it. The program does convert AbC to aBc but it will not convert if the same character in the string. It will convert the first character to the opposite case and apply it to the rest of the letters. For example, aBcb will convert to AbCb or AbCB will convert to ABCB. Below is my code. Any suggestions will be appreciated.
    HTML Code:
    private class CalculateButtonHandler implements ActionListener
       {
          //create convert button action
          public void actionPerformed(ActionEvent e)
          {
        	  //declare variables
        	  int i;
        	  String str;
    
                  //get string from text field
                  str = string1TF.getText();
          
                 //loop through string
                 for (i = 0; i < str.length(); i++)
                   {
    
            	   //determine if character is upper or lower and reverse
            	 if
            	(str.charAt(i) == Character.toUpperCase(str.charAt(i)))
                 str = str.replace(str.charAt(i), Character.toLowerCase(str.charAt(i)));
            	
                 else 
                	 str = str.replace(str.charAt(i), Character.toUpperCase(str.charAt(i)));
            		 
                    }
             
             	//show result
            	string2TF.setText(str);
          }
       }
    Last edited by sselasky1; February 16th, 2012 at 02:11 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Uppercase and Lowercase in a string

    Just thought of why it's not changing the same character to the opposite case. I thought by specifying the exact location it would only replace that character but it will still replace all the characters to that case. I've been trying to figure this out for the last two days so any suggestions will still be appreciated. Thanks

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Uppercase and Lowercase in a string

    Quote Originally Posted by sselasky1 View Post
    Just thought of why it's not changing the same character to the opposite case. I thought by specifying the exact location it would only replace that character but it will still replace all the characters to that case. I've been trying to figure this out for the last two days so any suggestions will still be appreciated. Thanks
    java.lang.String is immutable - you can't change it - so it might be better to tackle this problem one character at a time and build a new sequence of characters which you can then make into your result String. You can get an array of characters from a String with toCharArray() and use new String(char[]) to convert a char array back to a String. You *can* change the individual chars in a char array. Perhaps that might work better?

Similar Threads

  1. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. Making a string lowercase
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 14th, 2011, 02:49 PM
  4. ArrayList<String> convert to lowercase
    By s_mehdi76 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2010, 09:05 PM
  5. Listing the alphabet in lower and uppercase
    By Nemphiz in forum Object Oriented Programming
    Replies: 2
    Last Post: May 25th, 2010, 05:25 PM