This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
A -> N
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.
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.
Joined: Sep 12, 2011
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...
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
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com