-1

I turned a codingbat warmup exercise into a program on Eclipse. The exercise calls to take the last char of a word and tack it onto both the front and end of a word, e.g. "cat" → "tcatt."

First attempt:

I started with this set of code and received the error "void methods cannot return a value." After some research, it appears that it's simply that if there's only one main method, you cannot return a value.

Scanner input = new Scanner(System.in);

System.out.println("Enter a word: ");

String str = input.nextLine(); // user input

String last = str.substring(str.length() - 1);
return last + str + last;

Second Attempt:

I tried here adding a second method and renaming the second string at the bottom to str1, to correct a duplicate local variable error:

public static void main(String[] args) {

}

public String backAround(String str) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a word: ");

    String str1 = input.nextLine(); //user input

    String last = str1.substring(str.length() - 1);
    return last + str1 + last;

This code now displays no errors, but won't display anything and thus won't take any user input. What is the methodology to correctly get the user input and return the string?

user9503053
  • 107
  • 3
  • 15
  • "*This code now displays no errors, but won't display anything*" why do you think it should display anything? – Pshemo Aug 15 '15 at 17:34
  • You main method never calls backAround. Your main method does nothing so the program as a whole does nothing. – Mike Samuel Aug 16 '15 at 16:01

3 Answers3

0

You are confusing a function return with simply displaying text back to the user. If you do not understand functions yet, you can stick with modifying the main function, and simply print results back to the console

Scanner input = new Scanner(System.in);

System.out.println("Enter a word: ");

String str = input.nextLine(); // user input
String last = str.substring(str.length() - 1);
System.out.println(last + str + last);
mattm
  • 5,851
  • 11
  • 47
  • 77
0

So for a string method, you return a string value. SO you will have to have a string varable equal to that string value, then print it out.

      public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a word: ");
            String str1 = input.nextLine(); //user input

            String result = backAround(str1); 
            System.out.print(result); 

            }

            public String backAround(String str) {
                String last = str.substring(str.length() - 1);
                return last + str + last;
        }
Andrew Kralovec
  • 491
  • 4
  • 13
0

Your problem is your code is correct but never used. Change String backAround(String str) to String backAround() since you do not need the str param since it is never used. Then add to you main the following

System.out.println(backAround());

The main is the only function that will be run when you run your application. By adding the System.out.println(backAround()) line you tell your application it has to run the backAround function and then printing the returned String

ThomasS
  • 705
  • 1
  • 11
  • 30