Java Help !
Page 1 of 1
Subseven12




Posts: 4

PostPosted: Tue, 13th Nov 2007 19:11    Post subject: Java Help !
Hello,

How can i change a character like 'a' into 'b' ?

example:

if i type "Java" as input. It must change as output into: "Kbwb".

Like an cryptography.

Please can somebody help me ? I'm stuck with this problem.

Greetings
Back to top
[sYn]
[Moderator] Elitist



Posts: 8374

PostPosted: Tue, 13th Nov 2007 19:44    Post subject:
The easiest way is to read the text into an array.. create a for loop that runs through every element of that array filled with an IF statement for every letter of the alphabet ..

eg.

for i = 26..etc..
If array[i] = a then
array[i] = b;
else if array[i] = b then
array[i] = c;
else if... etc...
Back to top
swingman




Posts: 3602

PostPosted: Tue, 13th Nov 2007 19:50    Post subject:
Don't know if this is even possible in java but wouldn't it be easier to use the character code for each letter and just +1 to it and convert back?
Back to top
Subseven12




Posts: 4

PostPosted: Tue, 13th Nov 2007 19:51    Post subject:
That is possible but i dont know how.
Back to top
Twitchy




Posts: 13

PostPosted: Wed, 14th Nov 2007 00:06    Post subject:
Code:

StringBuilder aStringBuilder = new StringBuilder("Java");

for (int i = 0; i < aStringBuilder.length(); i++) {
     aStringBuilder.setCharAt(i,(char)(aStringBuilder.charAt(i)+1));
}
      
System.out.println(aStringBuilder.toString());
Back to top
sabalasa




Posts: 369
Location: EST
PostPosted: Wed, 14th Nov 2007 15:50    Post subject:
If you want to do it the LEET programmer way, which makes you irresistible to chicks by the way, you might also try it this way

Code:

import java.io.ByteArrayInputStream;


public class CharSwap {
   public static void main(String[] args) {
      if (args.length < 1) {
         return;
      }
      ByteArrayInputStream in = new ByteArrayInputStream( args[0].getBytes() );
      StringBuffer out = new StringBuffer();
      byte inChar;
      while ( (inChar = (byte) in.read() ) != -1) {
         out.append( new String( new byte[] { (byte) (inChar + 1) } ) );
      }
      System.out.println(out);
   }
}


rgds
Sabalasa
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group