-7

I have this homework from the school, however Im not sure Im doing wrong the second array does not takes the values of the first one, can you help me please?

public static void main(String[] rpp) {

        String[] word = new String[7];        
        word[0] = "D";
        word[1] = "E";
        word[2] = "N";
        word[3] = "T";
        word[4] = "I";
        word[5] = "S";
        word[6] = "T";        

        String[] inverseWord = new String[7];
        inverseWord[0] = "";
        inverseWord[1] = "";
        inverseWord[2] = "";
        inverseWord[3] = "";
        inverseWord[4] = "";
        inverseWord[5] = "";       


        for(int x = word.length;x <= 0;x--){
            for(int y = 0;y <= word.length;y++){
                inverseWord[y] = word[x];                
            }            
        }        
        System.out.print(Arrays.toString(inverseWord) + "\n");
    }
JMR
  • 765
  • 1
  • 8
  • 31
ripepe
  • 45
  • 5
  • 3
    Possible duplicate of [Reverse a string in Java](http://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – Frakcool Nov 26 '15 at 17:47
  • 1
    this is a duplicate of a dozen other reverse string questions, did you even look for an existing answer? –  Nov 26 '15 at 17:47
  • 1
    I think he is asking how to fix his code rather than asking for an alternative solution. – user3437460 Nov 26 '15 at 17:49
  • Common use is to name iterator values `i`, `j`, `k`, ..., not `x` or `y`, unless they have special meaning as coordinates. – Andreas Nov 26 '15 at 18:00

5 Answers5

2

Array-indexes start at 0. Which means that if the length is L, the last index in the array will be (L-1).

This means that instead of int x = word.length you need to write int x = word.length - 1 and instead of y <= word.length you need to write y < word.length or y <= word.length - 1.

Also, the condition in your outer loop should be x >= 0, not x <= 0.

RaminS
  • 2,208
  • 4
  • 22
  • 30
1
  1. You don't need two for loops
  2. x should be greater than equal to 0 not less than

public static void main(String[] rpp) {
    String[] word = new String[7];
    word[0] = "D";
    word[1] = "E";
    word[2] = "N";
    word[3] = "T";
    word[4] = "I";
    word[5] = "S";
    word[6] = "T";

    String[] inverseWord = new String[7];
    inverseWord[0] = "";
    inverseWord[1] = "";
    inverseWord[2] = "";
    inverseWord[3] = "";
    inverseWord[4] = "";
    inverseWord[5] = "";

    for (int x = word.length - 1; x >= 0; x--) {
        inverseWord[(word.length-1) - x] = word[x];
    }
    System.out.print(Arrays.toString(inverseWord) + "\n");
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Rishi Saraf
  • 1,644
  • 2
  • 14
  • 27
0

The condition on your outer for loop should be

x >= 0

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

Rewrite the for loop used for reversing using 2 variables.This is the simplest and most basic method.

 for(int i=word.length-1,int j=0;i>=0,j>=0;i--,j++) //loop for reversing
            {
                inverseWord[j] = word[i];
            }

    for(int i=0;i>=0;i++) // for displaying the inverseword array
    {
    System.out.print(inverseWord[j];
    }
Mathews Mathai
  • 1,707
  • 13
  • 31
0

You can use StringBuilder to append individual characters into a String. The String "DENTIST" can be broken up into characters of reverse order with the use of the charAt method in the String class and a for loop.

public class MainClass {

    public static void main(String[] args) {
        String word = "DENTIST";
        StringBuilder inverseWord = new StringBuilder();

        for (int i = word.length()-1; i >= 0; i--) {
            inverseWord.append(word.charAt(i));
        }
        System.out.println(inverseWord);

    }

}
Stinlang
  • 22
  • 5
  • TO be honest, if I already use `StringBuilder`, when I would use `String inverseWord = new StringBuilder(word).reverse().toString();` and I'm done. – Tom Nov 26 '15 at 21:41