0

EDIT** Thank you all for the help! It is much appreciated!

sorry for all of the advanced programmers that have to look at this and scoff at the newbie, but I'm trying.

Can anyone tell me how to call the method to reverse the string and print out the text that it returns? Much thanks, guys!

Here is my code

import java.util.Scanner;

public class Palindrome {

public static void main(String [] args) {
    System.out.println("Please enter the text you would like reversed.");
    String reverseText
    System.out.printlne(reverseText);       
}   

public String reverseString() {
    @SuppressWarnings("resource")
    Scanner s = new Scanner(System.in);
    String text = s.nextLine();
    String reverseText = new StringBuffer(text).reverse().toString();
    return reverseText;     
}

}
Joe D
  • 3
  • 2
  • 4
    How to call a method should be one the most fundamental things. You should start with some Java book or tutorial. In addition, what you're doing is not with the reverse String method is not creating a palindrome, but I guess this is just a part of your whole program? – UninformedUser Aug 08 '17 at 01:24
  • Yes I was planning on making a program that can reverse some text then make the user enter a palindrome. I will send the completed project once finished (may be a while lol). The reason I dont have a java book on hand is that it is not currently the school year and I was waiting until I got back into school to ask my old java teacher if I could have one of the textbooks to take home. hopefully it is very helpful! – Joe D Aug 08 '17 at 01:40

3 Answers3

0

Change your main method to be

public static void main(String [] args) {
    System.out.println("Please enter the text you would like reversed.");
    System.out.println(new Palindrome ().reverseString ());
}

As the method is not static, you need to create an object of type Palindrome before you can use it. <scoff></scoff>

As other's have mentioned it is also possible to have the reverseString method being a static method, removing the necessity of creating a Palindrome object. Consult this link

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

There are a few things you could do. Because your method reverseString is not marked with the keyword static, it is an instance method by default. This means that to use it, you need to create a Palindrome object.

To use it as an instance method:

public static void main(String[] args)
{
    System.out.println("Please enter the text you would like reversed.");
    System.out.println(new Palindrome().reverseString());
}

Or, you could simply declare it as a class method using the keyword static and not have to go through the trouble of creating an object to use the method (see below).

To use it as a class method:

Change your reverseString() method's header to as follows:

public static String reverseString() {

And then from your main:

public static void main(String [] args) {
    System.out.println("Please enter the text you would like reversed.");
    System.out.println(reverseString());
}
Luke Thistlethwaite
  • 428
  • 1
  • 4
  • 17
0

There are a few problems with your code.

  • You don't need to initialize a new string in your main method since it is handled in your method. So you can remove line 5, as well as the reference to reverse text in line 6 (replace it with a call to your reverseString method).
  • You have a typo in line 6: System.out.printlne() (remove the last 'e')
  • Because you are not creating any class instances, you should call the method statically (see code below)

Code:

import java.util.Scanner;

public class Palindrome {

 public static void main(String [] args) {
     System.out.println("Please enter the text you would like reversed.");
     System.out.println(reverseString());
 }

 public static String reverseString() {
     @SuppressWarnings("resource")
     Scanner s = new Scanner(System.in);
     String text = s.nextLine();
     String reverseText = new StringBuffer(text).reverse().toString();
     return reverseText;
 }
}
  • Yes, it does appear that since he has a `main` method, he does not want to use it as an instance method.. But there is a possibility he might try to create a Palindrome object and call the reverseString() method from it, so in that case it is best to say how to do it non-statically. – Luke Thistlethwaite Aug 08 '17 at 01:34
  • I agree he may want to do it that way. It all just depends on what application you are using this for. I consider this to be a more simple way to test the concept though. – Robert Swanson Aug 08 '17 at 01:38