Results 1 to 5 of 5
  1. #1
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default What is the answer yo my problem with this code

    this is the code
    Java Code:
     // this code should replace any occurence of the middle char with the nex char in unicode   
        String str;
        int x,z,w;
        char y;
        System.out.print("Please Enter String \n");
        str=console.next();
        x=str.length();
        y=str.charAt(x/2);
        // Now The problem that I want to get the next char in unicode
        str=str.substring(x/2, x);
       // I will use the replace method

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    There are two replace methods in the String class api (javadocs Method Summary). One takes single char arguments. This won't work because hex strings will likely be longer than a single char. The other method takes CharSequence interface arguments. Looking up the CharSequence interface we see it has four methods and is implemented by (look up top under "All Known Implementing Classes:" the String class. Therefore, String must implement the methods defined in the interface. So we can use the subSequence method to convert from String to CharSequence for the replace method.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            String s = "hello";
            System.out.println("s = " + s);
            int len = s.length();
            int index = len/2;
            char c = s.charAt(index);
            int n = (int)c;
            String hex = Integer.toHexString(n);
            System.out.println("hex = " + hex);
            CharSequence target = s.subSequence(index, index+1);
            CharSequence replacement = hex.subSequence(0, hex.length());
            s = s.replace(target, replacemment);
            System.out.println("s = " + s);
        }
    }

  3. #3
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    thanks but i don't mean this
    i want the program to do as the next example:
    ex: abccc
    the output should be abddd

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
            String s = "abccc";
            System.out.println("s = " + s);
            int len = s.length();
            int index = len/2;
            char c = s.charAt(index);
            int n = (int)c;
            c = (char)(n+1);
            System.out.println("c = " + c);
            String target = s.substring(index, index+1);
            String replacement = String.valueOf(c);
            s = s.replace(target, replacement);
            System.out.println("s = " + s);

  5. #5
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 12:15 AM
  2. Problem with code
    By jvasilj1 in forum New To Java
    Replies: 5
    Last Post: 02-02-2008, 08:34 AM
  3. Problem with code
    By oregon in forum New To Java
    Replies: 3
    Last Post: 08-05-2007, 05:57 PM
  4. Problem with zero in my code
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:39 AM
  5. Problem with my first code
    By paul in forum New To Java
    Replies: 2
    Last Post: 07-26-2007, 04:09 PM

Posting Permissions

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