0

I'm trying to print a statement vertically, and then backward vertically with two classes so I can practice multiple skills. However, frustratingly, I cannot get my program to work and keep getting a "string index out of range error". I'm not sure if I'm miscalling my functions because I am new to Java.

class Main {
  public static void main(String[] args) {

    MyString.verPrint("treasure");
    MyString.backPrint("treasure");

  }
}



public class MyString {
    
    public static String verPrint(String x){
      int i = x.length();
    while(0 < i){
      x = x.charAt(i) + "\n";
      i++;
    }
      return(x);
    }
      
    public static String backPrint(String x){
        int i = x.length() - 1;
      while(i >= 0){
          i--;
          x = x.charAt(i) + "\n";
        }
      return(x);
    }
}

ayla s
  • 43
  • 6
  • How about you look at your logic in `verPrint`, follow along with it, and write down what values `i` is going to take. – khelwood Nov 11 '20 at 06:07
  • Also, neither of your `print` methods does any printing, so if they were to work, you would not see any output. – khelwood Nov 11 '20 at 06:10

3 Answers3

0
String s="HELLO";
        char[] y=s.toCharArray();
        for (char x : y) {
            System.out.println(x);
        }

OUTPUT
H
E
L
L
O

String S="Hello";
        S=new StringBuffer(S).reverse().toString();
        System.out.println(S);
char[] y=S.toCharArray();
            for (char x : y) {
                System.out.println(x);
            }

You know output

Adarsh s
  • 128
  • 11
0

The problem with your solution is you are updating the input string in the first iteration by assigning a character and a new line to it. Hence, it doesn't work for the latter iterations. I've made some changes in your snippet. You may follow it -

public class Main {
  public static void main(String[] args) {

    System.out.println(MyString.verPrint("treasure"));
    System.out.println(MyString.backPrint("treasure"));

  }
}

class MyString {

  String x;
    
    public static String verPrint(String x){
      int i = x.length();
      String y = "";
      int j = 0;
      
    while(j < i){
      y += x.charAt(j) + "\n";
      j++;
    }
      return(y);
    }
      
    public static String backPrint(String x){
      int i = x.length() - 1;
      String y = "";
      
    while(i >= 0){
      y += x.charAt(i) + "\n";
      i--;
    }
      return(y);
    }
}
Afif Al Mamun
  • 199
  • 1
  • 11
  • Thank you so much! I see the problem with printing now. I always forget with the static method that it returns and doesn't print. I see why a new variable had to be put in because the way I did it I think would have started it on the last letter which wouldn't have worked. Thank you again! – ayla s Nov 11 '20 at 06:28
  • Anytime! If it worked for you please mark it as the answer. Thanks! – Afif Al Mamun Nov 11 '20 at 06:48
0

your conditions have problem, while checking for the length and index of x string parameter:

public class Main {
    public static void main(String[] args) {

        System.out.println(verPrint("treasure"));
        System.out.println("-----------");
        System.out.println(backPrint("treasure"));
    }

    public static String verPrint(String x) {
        int i = 0;
        String result = "";
        while (i < x.length()) {
            result += x.charAt(i++) + "\n";
        }
        return result;
    }
    
    public static String backPrint(String x) {
        int i = x.length() - 1;
        String result = "";
        while (i >= 0) {
            result += x.charAt(i--) + "\n";
        }
        return result;
    }
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36