MikeLI MikeLI - 3 months ago 8
Java Question

Want to change a -z to b-z


  1. Question

    My input is
    aaasssuuu
    , but my output is
    aaasssvvv
    ;
    What I want is
    bbbtttvvv
    . So what's wrong with my code ?

  2. Code

    class Solution{
    public static void main(String[] args) {
    String str = "aaasssuuu";
    System.out.println(functionalShiftLetters(str));
    }

    public static String functionalShiftLetters(String str){
    String ss = new String();
    for(int i =0; i<=str.length()-1 ;i++)
    sb = str.replace((char)str.charAt(i), (char) (str.charAt(i)+1));

    return ss;
    }

    }


Answer

With Java 8:

  public static void main(String[] args) {
    String str = "aaasssuuu";
    str.chars() 
        .mapToObj(x -> (char) (x + 1)) 
        .forEach(System.out::println);
    }

with java 6:

char[] w = str.toCharArray();

    for (char c : w) {
        System.out.println((char) (c + 1));
    }
Comments