-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.

5
  • "revrse" -> Look at how you have called the method and how it has been defined Commented Jul 16, 2014 at 14:16
  • 1. does you code compile? 2.you can reverse you string easily using a StringBuilder. Commented Jul 16, 2014 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(). Commented Jul 16, 2014 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 Commented Jul 16, 2014 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] Commented Jul 16, 2014 at 14:42

2 Answers 2

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;
 }  
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.