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 10 of 10

Thread: Converting letter casing in strings

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting letter casing in strings

    I am trying to figure out how to alter specific characters in a string. Say I were to enter a string in that contains a lowercase "i".
    How could I command java to convert the lowercase "i" to an uppercase "I" before printing the string?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Converting letter casing in strings

    Check out the API for Character and String. They both contain methods that might be useful here.

    Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I will give it a look when I get to a computer thanks!! if anyone else has any feedback or the answer to my question I would appreciate it if they'd chime in. Other resources like the API would be appreciated too.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Converting letter casing in strings

    Quote Originally Posted by SeanEE89 View Post
    I will give it a look when I get to a computer thanks!! if anyone else has any feedback or the answer to my question I would appreciate it if they'd chime in. Other resources like the API would be appreciated too.
    I did give you a link to the API, which contains links to the String and Character APIs. Hint: You're looking for a method that converts Strings to upper case, right?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting letter casing in strings

    You're close, but I am aware of the toUpperCase method. I'm trying to convert a specific character though; not an entire string of characters. If I were to enter the word "laugh" I would want it to alter it to say "Laugh" with a capitalized L, and if a capitalized "L" was entered in the first place for it to do nothing.

    I have tried to use lines like the ones I'll put down below, but I have not found anything that is working... I realize both lines won't work if they're both inside the if statement. I included both to give examples of what I have been trying incase there was any confusion. I am sure I'm missing something, but I can't figure out what it is exactly...

    String input = "laugh";

    for(int i = 0; i < input.length(); i++){

    if(input.charAt(i) =='l'){

    input.replace('l' , 'L');
    //did not work

    input.charAt(i) = 'L';
    //did not work either

    continue;

    }//end if

    }end for loop

  6. #6

    Default Re: Converting letter casing in strings

    I think what you are asking for is impossible. In Java, strings are immutable, i.e., you cannot access the array of characters and edit them. You must create a new String and reflect the changes you want in that new instance and then assign it to the proper reference.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting letter casing in strings

    Quote Originally Posted by kenster421 View Post
    I think what you are asking for is impossible. In Java, strings are immutable, i.e., you cannot access the array of characters and edit them. You must create a new String and reflect the changes you want in that new instance and then assign it to the proper reference.
    What methods would I have to use in order to accomplish this?

    If this is not possible why is there a replace method for characters? Just wondering, because the examples I've seen of it lead me to believe what I'm doing is much easier then it seems.

    Would converting the string into a character array and then making the changes be the best course of action, and then converting it back to a string if that too is possible?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Converting letter casing in strings

    Quote Originally Posted by SeanEE89 View Post
    What methods would I have to use in order to accomplish this?
    The toUpperCase() methods are really the only thing you need.

    Quote Originally Posted by SeanEE89 View Post
    If this is not possible why is there a replace method for characters? Just wondering, because the examples I've seen of it lead me to believe what I'm doing is much easier then it seems.
    It is possible, and even quite simple. What Kenneth was saying is that String are immutable- that means you can't change the original String. But you can certainly store the result in a new String. For example, to take a substring, I can't do this:

    String someString = "this is an example String";
    someString.substring(2, 5);
    //someString is still "this is an example String"

    Instead, I have to do this:

    String someString = "this is an example String";
    String otherString = someString.substring(2, 5);
    //someString is still "this is an example String"
    //otherString contains the substring

    That's how all the String methods work, including replace and toUpperCase.


    Quote Originally Posted by SeanEE89 View Post
    Would converting the string into a character array and then making the changes be the best course of action, and then converting it back to a string if that too is possible?
    I don't think so.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting letter casing in strings

    Quote Originally Posted by KevinWorkman View Post
    The toUpperCase() methods are really the only thing you need.



    It is possible, and even quite simple. What Kenneth was saying is that String are immutable- that means you can't change the original String. But you can certainly store the result in a new String. For example, to take a substring, I can't do this:

    String someString = "this is an example String";
    someString.substring(2, 5);
    //someString is still "this is an example String"

    Instead, I have to do this:

    String someString = "this is an example String";
    String otherString = someString.substring(2, 5);
    //someString is still "this is an example String"
    //otherString contains the substring

    That's how all the String methods work, including replace and toUpperCase.




    I don't think so.
    How could i use the toUpperCase method to accomplish this?

    As for using subStrings would I have to use two incase I were to enter a word where the 'L' might be inside of the given word like if I were to enter the words "world" or "floor" for example to have them displayed as "worLd" and "fLoor".

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting letter casing in strings

    String input = "little";

    for(int i = 0; i < input.length(); i++){

    else if(input.charAt(i) == 'l'){

    output = input.replace('l', 'L');

    }//end l to L conversion

    }//end for loop

    figured it out. the output is changed from "little" to "LittLe".

Similar Threads

  1. letter pattern
    By severus1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 4th, 2011, 01:24 PM
  2. Excel Column Letter Referencing
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 21st, 2010, 03:47 PM
  3. Random Letter problem please help!
    By xs4rdx in forum Java Theory & Questions
    Replies: 1
    Last Post: March 28th, 2010, 11:27 AM
  4. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 07:01 AM