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?