SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Help with Java String Replacement program

  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Malaysia
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post 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....

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Malaysia
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    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

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Malaysia
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    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<Character, Character> map = new HashMap<Character, Character>();

    // 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;

    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •