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?
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.
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?
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!
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.
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!
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
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?
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:
That's how all the String methods work, including replace and toUpperCase.
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!
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".
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".