• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing characters in a string?

 
Tina Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a little help with an assignment I'm working on..
given a string entered into a text box by a user.. lets name this string text.

using a for loop to go through each char in the string.. I want to take that letter and replace it with a character 13 places after it.
for example:
A -> N
B-> O
C-> P

M-> Z
N-> A (There is no letter 13 places after N, so we “wrap around” to the beginning of the alphabet)

if the char happens to be a number or symbol.. I will simply leave it as.

there is no method in the string or character class that I can fin that can help me with this.
I do not want to use an Array or ArrayList.
an I obviously cant do a + 13.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what excatly causes problem?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tina. Welcome to The Ranch!

Tina Davis wrote:an I obviously cant do a + 13.


You sure about that? chars are actually integer types, so once you've got the letter into a char variable, you can just add numbers to it. It's a useful trick for things like this.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excatly. You can do this adding just like this:
 
Tina Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Tina. Welcome to The Ranch!

Tina Davis wrote:an I obviously cant do a + 13.


You sure about that? chars are actually integer types, so once you've got the letter into a char variable, you can just add numbers to it. It's a useful trick for things like this.


yep. "type mismatch, cannot convert from int to char."
 
Tina Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zandis Murāns wrote:excatly. You can do this adding just like this:


When I do it I get an error telling me I cannot convert from an int to a char.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might need a cast to get it to allow the assignment (because a char + int is considered an int). Although try the += method, as that has a built-in implicit cast.
 
Tina Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the += worked...
however.. as I go through the string using a for loop one char at a time.. I want to determine if there is an int.. if so.. then I change nothing..
if the original letter is a capital.. then the new char should be capital.. and I want to test for whitespace as well..
so i know I need a few if statements...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you have enough info to make a go at it. Be sure to use code tags when posting your solution.
 
Tina Davis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually... although the += 13 does work... it does not work CORRECTLY.
for one, if the char is the letter z. then the result ends up being ‡a weird symbol i cant paste here, and if its a capital Z then the result is lower case g when it should be capital M.

I know I need some "if" statements.
If the character is not a letter, leave the character unchanged
If the character is an uppercase letter, replace it with the uppercase letter that’s 13 positions after it in the alphabet, wrapping around to the beginning of the alphabet if necessary
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the ASCII table. As you can see, if you add 13 to the char 'z', resulting char will be 122 + 13 = char at index #135.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of +=; there are peculiarities about it because it involves both widening and narrowing conversion, so its behaviour is only reliable when used on ints and longs (and probably, but I am not certain) on floating-point numbers.
Whoever said "casting" was correct.
Don't use 122 for 'z'; use 0x7a. The sooner you learn hexadecimal arithmetic, the better. You can look up ASCII charts in all sorts of places, but Java™ doesn't use ASCII. It uses Unicode. You can find the values here and here. You will notice the values in the first link are identical to ASCII, however. If you add 'z' (0x7a) to 0x0d (13) you get 0x87, and you can look up on the second link what that means. If it isn't a printing character, you needn't expect it to appear on the command line. This html editor uses ‡ for (char)0x87. You can get it to appear in a Java™ String literal like this:
Are you sure you meant + 13 and not - 13?

Anyway, Strings are immutable, so you needn't expect to replace characters in them in the first place. You can get a copy by dumping the String into a char[] or putting it into a StringBuilder. You can use the % operator for wrapping round or even choosing between + 13 and -13, but I think you want to break this job into small bits.
  • Get a pencil and paper and a String like "Campbell Ritchie and Zandis Murāns are trying to help Tina Davis 89374943^%$&^^(%#']?"
  • Write it out as a char[]
  • Look at each char and its Unicode value.
  • Work out what you get with c % n
  • Work out what the best value for n above would be
  • Consider what modification to make to the char
  • Create a new String from the char[]
  • But don't do all those things. Do them one at a time, and print out the result after each stage.
    Also look in the Character class and see whether you are allowed to use its many useful methods.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic