-3

i have wrote java function reversing user input but it nor working and i don't have any idea where is the problem and how to solve it.

import java.util.Scanner;

class Reverse{
public static void main(String args[]){
    Scanner word = new Scanner(System.in);
    System.out.print("type the word here to check if it is palindrome: ");
    String n = word.nextLine();
    char[] let = n.toCharArray();
    System.out.print(revrse(i, let));
    // for (int i=let.length-1; i>=0; --i){
        // System.out.print(let[i]);
    // }

 }
 public static int reverse(char[] let){
    for (int i=let.length-1; i>=0; --i){
        return let[i];
    }  
 }

}

how to make this code to work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Garegin
  • 61
  • 1
  • 9
  • "revrse" -> Look at how you have called the method and how it has been defined – anirudh_raja Jul 16 '14 at 14:16
  • 1. does you code compile? 2.you can reverse you string easily using a StringBuilder. – Shailesh Aswal Jul 16 '14 at 14:19
  • Also, your `reverse` method simply returns the ordinal of the last character in the character array, it does not reverse it. As @Shail016 mentioned you could just use `new StringBuilder(n).reverse().toString()`. – PermaFrost Jul 16 '14 at 14:22
  • sorry I am beginner and I am not totally understanding what you recommended to do, can you give more deep explanations to your suggestions – Garegin Jul 16 '14 at 14:27
  • If you just need to check for a `String` is a `Palindrome` or not, why to reverse it. Simply take a variable, initialize it to `say i = 0` and second one` initialize it to `say j = let.length - 1` start the loop from `0` to `< let.length / 2` and compare `let[i] with let[j]` – nIcE cOw Jul 16 '14 at 14:42

2 Answers2

3

Well first you use

 System.out.print(revrse(i, let));

But you defined the method called

int reverse(char[] let)

so the method reverse isn't called in the code you posted. Second thing your method reverse(char[]) isn't returning the reversed String but simply return an integer corresponding to last character of the original String.

Try this code in your reverse method ():

    String str = "This is a try";
    char[] arr = str.toCharArray();
    char[] temp = new char[arr.length];

    for (int i = 0; i < arr.length; i++) {
        temp[i] = arr[arr.length - 1 - i];
    }

EDIT

You should try with that:

class Reverse{
   public static void main(String args[]){
   Scanner word = new Scanner(System.in);
   System.out.print("type the word here to check if it is palindrome: ");
   String n = word.nextLine();
   char[] let = n.toCharArray();
   System.out.print(reverse(let));
   // for (int i=let.length-1; i>=0; --i){
      // System.out.print(let[i]);
   // }

 }
 public static char[] reverse(char[] let){
   char[] reversed = new char[let.length];
    for (int i = 0; i < let.length; i++) {
        reversed[i] = let[let.length - 1 - i];
    }
  return reversed;
 }  
}
Marco
  • 66
  • 1
  • 1
  • 6
2

Your reverse(char[] let) returns the last letter. You should append your letters to a String variable in the for loop and return it after the loop.

Also, the following is wrong in your code:

System.out.print(revrse(i, let));

The method parameters and the method's name don't match.

Advice:

Probably the best way would be to pass the input string to a

public static Stringreverse(String input) {
    String reversed = new String("");

    // do reversing in for loop here
    // ...

    return reversed;
}

method and handle the reversing inside it. In this way, your function deals with a given task and your main() function stays clean. You can print the return value of this in the main function.

kruze
  • 361
  • 1
  • 3
  • 8