moving chars

+1 stian hjornevik · November 16, 2014
hey, mayby I posted this two times now, if so Im sorry.

As part of a larger program I'm working on I need a bunch of letters to jump two places in the alphabet. After that I will need them to jump back, but if I figure out how to make them jump forward I'll figure out the rest myself. 
Anyone know how to do this?

newText = "";
text = keyboard.nextLine();
for(int i=0;text.length();i++) {

// newText = text, but with all letters pushed two slots

}

System.out.println(newText);

...........

input:    zebra
output:  bgdtc

Post a Reply

Replies

Oldest  Newest  Rating
-1 Homer Simpson · November 17, 2014
Here's what I did, I converted my string into a char array and then i type casted the char at each element into an int. That way it was equal to its ascii value. So then I just added two to it and converted it back to a string. Heres the code:

import java.util.Scanner;

public class Practise { 
     
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String word = "";
   
    word = input.nextLine();
   
    System.out.println("" + convertStringToChar(word));
    }
    
    static String convertStringToChar(String word){
    String newWord = "";
    char temp[] = new char[word.length()];
   
    for(int i = 0; i < temp.length; i++){
    temp = word.charAt(i);
    temp = (char) ((int) temp + 2);
    newWord += temp;
    }    
    return newWord;
    }
    
}

I entered in zebra and it returns |gdtc.
Theres a problem with z because it also increases the ascii value by 2 making it equal to | but this is a relatively easy fix. Let me know if you need anymore help. :]
  • 1

Java / Android Development

107,267 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator