-1

Like to know how to reverse a string value (1 word) which is pre-declared in the program. I mean not using user input or scanner.

Like to reverse a word "TRAIN" which is pre-declared in the program.

Have tried the below program but no results and no error also.

//  QUERY PROGRAM NOT RUNNING - NO RESULT, NO ERROR.
//  STRING REVERSE PROGRAM USING ARRAY
package abnpackage;

class Play {

    void REVERSE (){
        String [] INPUT_WORD = {"T","R","A","I","N"};
        int Q;
        for(Q=INPUT_WORD.length-1; Q>=0; Q=Q--);
            System.out.print ("REVERSE VALUE" + INPUT_WORD[Q]);
    }

    public static void main(String[]args){
        Play PL = new Play();
        PL.REVERSE();
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
badri
  • 31
  • 8

4 Answers4

2

Problem in Q=Q-- and ; symbol after for cylce. Try this:

class Play{
    void REVERSE (){
        String [] INPUT_WORD = {"T","R","A","I","N"};
        int Q;
        for(Q=INPUT_WORD.length-1; Q>=0; Q--) {
            System.out.print(INPUT_WORD[Q]);
        }
    }
    public static void main(String[]args){
        Play PL = new Play();
        PL.REVERSE();
    }
}
Alexey Nikitin
  • 604
  • 7
  • 22
1

I'd like to offer a few suggestions.

  1. Indent your code. It not only makes it easier for you to follow, but makes it easier for others to read your code.

  2. Naming conventions. Use Title case for classes, camelCase for both variables and methods, and UPPER_CASE for constants.

  3. Strings and characters. A String can be decomposed into an array of characters with the built-in method, String.toCharArray(). A character array is mutable, so is often used as an intermediate structure when converting a String from one state to another for tasks like ciphers or interview problems.

  4. Encapsulation. If you can make your methods use only what is submitted to them through their method signature, and only output their return value, it's usually best. Prefer passing values over referencing constants in your utility methods to make them easier to follow.

    package abnpackage;
    class Play {
      private static final String INPUT_WORD = "TRAIN";
    
      private String reverse(String word) {
        char[] letters=word.toCharArray();
        StringBuilder sb=new StringBuilder();
        for (int q=letters.length-1; q>=0; q--) {
          sb.append(letters[q]);
        }
        return sb.toString();
      }
    
      public static void main(String[]args) {
        Play play = new Play();
        System.out.println("REVERSE VALUE: " + play.reverse(INPUT_WORD));
      }
    }
    
phatfingers
  • 9,770
  • 3
  • 30
  • 44
0
class Play {
void REVERSE() {
    String[] INPUT_WORD = {"T", "R", "A", "I", "N"};
    String[] OUTPUT_WORD =new String[INPUT_WORD.length];
    int length = INPUT_WORD.length;
    int i = 0;
    while(--length>=0){
        OUTPUT_WORD[i++] = INPUT_WORD[length];
    }
    System.out.println(Arrays.toString(OUTPUT_WORD));
}

public static void main(String[] args) {
    Play PL = new Play();
    PL.REVERSE();
}
}
Devram Kandhare
  • 771
  • 2
  • 8
  • 20
0

Your code is entering an endless loop because of the assignment "Q=Q--"

for(Q=INPUT_WORD.length-1; Q>=0; Q=Q--);

It should instead be

Q--

without a semicolon at the end.

If the code runs successfully, it will print the words "REVERSE VALUE" repeatedly prior to printing each character in reverse.

System.out.print ("REVERSE VALUE" + INPUT_WORD[Q]);

So you will want to keep the text in reverse prior to printing the whole statement at the end of the execution of the for loop.

What is the reason to use array of String instead of just String? Since it's not mentioned as a requirement, I'm suggesting the following as an alternative solution:

    public class Play {
    static void reverse(){
        String inputWord = "TRAIN";
        char[] toStrArray = inputWord.toCharArray();
        char[] revisedInput = new char[inputWord.length()];
        int i = 0;
        for(int q=toStrArray.length-1; q>=0; q--){
            revisedInput[i]=toStrArray[q];
          i++;
        }
        System.out.print ("REVERSE VALUE: " + new String(revisedInput));
    }

    public static void main(String[]args){
        //Play PL = new Play();
        //PL.REVERSE();
      reverse();

    }
}

Note: You can declare the method reverse as a static method. By doing this you don't have to create an object before calling it. Hope this helps.

yoges nsamy
  • 1,275
  • 1
  • 16
  • 29