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: Switching letter-cases from user input; why is not working?

  1. #1
    Junior Member Kimiko Sakaki's Avatar
    Join Date
    Mar 2011
    Location
    Toronto
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switching letter-cases from user input; why is not working?

    Hey guys,

    So I just started learning Java this year so you can say I'm fresh off the boat. Though I've learned programming concepts before (from Turing). Right now, I'm experiencing some difficulty in comparing/checking if a certain character is upper-case or lower-case.

    This simple program just asks for the user to input a word/phrase and whatever letter is lower-case, I change it to upper-case and whatever letter is upper-case, I change it to lower-case.

    import java.util.*;
    class UpperLower {
        public static void main() {
            String userInput;
     
            Scanner input = new Scanner (System.in );
            System.out.println ("Please enter something...:");
            userInput = input.nextLine();
            String copyInput = userInput;
     
            for (int num = 0; num < userInput.length(); num++) {
                if (Character.isUpperCase(userInput.charAt(num))){
                    (userInput.substring(num)).toLowerCase();
                } else {
                    (userInput.substring(num)).toUpperCase();
                }
            }
     
            System.out.println (userInput);
        }
    }

    There seemed to be no compiler error however, when I tested my program, the letters' case were still unchanged. I'm not sure what is wrong with my code (perhaps it has to do with my if-condition?).

    Please give this a try and see what is wrong. I really do appreciate your efforts, time and help. Thank you very much in advance!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Switching letter-cases from user input; why is not working?

    Strings in Java are immutable. Thus, every time you make a change to them you're actually creating a new string with that change. This can be remedied by re-assigning back the new string to the old variable (be careful that you get the whole string and not just the part you changed).

    If you want a mutable version of a String, use the StringBuilder class.

  3. #3
    Junior Member Kimiko Sakaki's Avatar
    Join Date
    Mar 2011
    Location
    Toronto
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switching letter-cases from user input; why is not working?

    Thank you for the help! I'll try and see what I can do with the StringBuilder class.

Similar Threads

  1. user input strings to vectors
    By vudoo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 7th, 2011, 10:30 PM
  2. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  3. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM
  4. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM

Tags for this Thread