-1

I have written a piece of code to reverse a string in Java. However, its showing multiple errors and I wish to understand where it is that I am going wrong. I know that there are alternative methods of reversing a string. However, I want to know where I am going wrong with my code.

public class RevString {
public static void main(String[] args)
{
    public Reverse (String str)
    {
        int len = str.length();
        String rev;

        for (int i = 0; i <= len; i++)
        {
                rev = str[i] + rev;
            }
            System.out.println(rev);    
        }
        Reverse("Canyon");
    }
}

Errors:

Multiple markers at this line
    - Syntax error on token ")", ; expected
    - Syntax error on token "(", . expected
    - Reverse cannot be resolved to a type
    - Illegal modifier for parameter str; only final is 
The method Reverse(String) is undefined for the type 
 RevString

Could someone provide me with a resolution?

Shah Quadri
  • 219
  • 1
  • 2
  • 11

4 Answers4

2

There are many errors in your code :

  • For loop condition should be i < len
  • String rev should be initialized to "" (empty string), else it will throw error when you try to append another string to it.
  • You can't access characters in a string using str[i], use str.charAt(i) instead.
  • You are trying to initialize a function (Reverse) inside another function (main), you must initialize it outside the main function.

Also, here is a simple one liner for string reversal:

new StringBuilder(str).reverse().toString()

A good tip might be to use the StringBuilder class whenever you want to do any kind of string manipulation in Java.

shaahiin
  • 1,243
  • 1
  • 16
  • 26
1

Your code has many issues:

  • You are declaring the method Reverse() inside the main method.
  • You also need to initialize rev to an empty string.
  • You can use str.charAt(i) to access each character of the string.
  • Your for loop goes beyond the string if you use i <= len; so it should be i < len;.
  • Your Reverse() method should be static since you are calling it in main method (which is static)

Here is working code.

public class RevString {
    public static void main(String[] args) {
        Reverse("Canyon");        
    }

    public static void Reverse (String str) {
        int len = str.length();
        String rev="";

        for (int i = 0; i < len; i++) {
            rev = str.charAt(i) + rev;
        }
        System.out.println(rev);    
    }
}
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
0

Please see the below code:

public class Hello {

    public static String reverse (String str){
        int len = str.length();
        String rev="";

        char[] strArray = str.toCharArray();

        for (int i = 0; i < len; i++)
        {
            rev = strArray[i] + rev;
        }

        return rev;
    }

    public static void main(String[] args) {

        String result = reverse("Canyon");
        System.out.println("Reversed String: " + result);
    }
}
S. M. Mohiuddin
  • 378
  • 3
  • 10
0
public class reverseString
{
  public static void main(String[] args)
  {
       System.out.println("Welcome to the the string reverser.");
       System.out.println("Here is where a person may put is a sentence and the orintation" +
                         " of the words is reversed.");
       Scanner keyboard = new Scanner(System.in);
       String word = keyboard.nextLine();
       int lengthOf = word.length();
       int j = 0;
    
       char loopStr;
       String LoopStr;
       String Nstr = "";
    
       for(int n = lengthOf; n >0 ;n--)
       {
           j = n;
           LoopStr = word.substring(j-1,j);
           Nstr = Nstr + LoopStr;
       
       }   
            System.out.println(Nstr);
   }
}
Hamish
  • 11
  • 1
  • Try this if you are a noob like me. – Hamish May 28 '21 at 17:53
  • A code-only answer is not high quality. What does your code do? How does it fix the problem? If you could explain rather than just presenting code, this answer would be more helpful. – Stephen Ostermiller May 28 '21 at 17:59
  • All this code does, it takes in a string for an example, like "my name is mud" then the output is "dum si eman ym". It reverses the string. This program does not place words in backward order. The code is not object-orientated, or array based because the assignment is before that. – Hamish May 28 '21 at 18:18