Help with Java String Replacement program

Please help I need to urgently write a program that will replace every character in a srting with a character which is found next to that character in your keyboard(with few asumptions and other rules…)eg. replace q with w ,t with y ,]with a asume shift is off and caps lock is off…

First, create a Map that will act as your ‘translator’, the keys will be the letters of the alphabet, the values will be the translated letter.

Then loop through the String one character at a time, using each character to retrieve it’s translated value from the Map.

Retain each translated value in a String or StringBuffer and return the translated String when you finish looping.

thanks for your response, please I am a beginning programmer at the moment I don’t know how to do what you have ask me to do, “map - translator” but i think the loop will not be a problem. please can you help me with the code for the map. thanks

is it something of this nature please help me complete this code please thanks
import java.util.*;

public class Map{


    • public static void main(String args){

        • Scanner input = new Scanner(System.in);

        • // an array of Map<String, Integer>
        • HashMap<String, Integer> hmsi= new HashMap[4];
        • // pupulate it * * * *
        • for (int i= 0; i < hmsi.length; i++)
        • hmsi[i]= new HashMap<String, Integer>();
    • }

}

Terdia

I’m assuming you’re using Java 6.

First you’ll need a Map setup to accept Characters for both the keys and values:

HashMap<Character, Character> map = new HashMap<Character, Character>();

Then load the map with the all the letters on your keyboard, along with their translated values as according to your assignment’s rules:

// for example
map.put( ‘a’, ‘s’ );
map.put( ‘b’, ‘n’ );

map.put( ‘z’, ‘x’ );

Now, when you ‘ask’ the map for the value of a letter, it will return the translated value:

map.get( ‘b’ ); // will return n

You can even get values ‘on the fly’:

map.get( new String( “apple” ).charAt( 0 ) ); // returns s

Thanks Alot it worked very well. this is it

import java.util.*;

public class Map{

public static void main(String [] args){

    //create a scanner object to read input fron users
    Scanner input = new Scanner(System.in);

    // a Map setup to accept Characters for both the keys and values
   HashMap&lt;Character, Character&gt; map = new HashMap&lt;Character, Character&gt;();

    // pupulate it with all the other keyboard value that you need
    //for example {, } - s,d - x, c

    map.put( 'a', 's' );
    map.put( 'b', 'n' );
    map.put( 'z', 'x' );
    map.put( 'q', 'w' );
    map.put( 'e', 'r' );
    map.put( 'z', 'x' );
    map.put( 'c', 'v' );
    map.put( 'd', 'f' ); //enter remaining character

    //prompt user to enter character
    System.out.println("Enter character to get the next keyboard value");

    //read the character
    ClientInput = input.next();

    //print out the next keyboard character according to your the map above
    System.out.println(map.get( new String( ClientInput ).charAt( 0 ) ) + " is the next character after " + ClientInput);


}

static String ClientInput;

}