Returning reversed string? Help please.
Hey everyone I'm missing something small I'm trying to make a code to have you put anything in you like and then the output be the reverse of that here is the code.
Code Java:
import java.util.Scanner;
public class Reversed {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Reversed.reverseString(String);
}
public static String reverseString(String S){
Scanner Console = new Scanner(System.in);
System.out.println("What ever you enter will be reversed");
String str1 = Console.nextLine();
String str2 = reverseString(str1);
System.out.println(str2);
return S;
}
}
I get this error "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
S cannot be resolved to a variable
at Reversed.main(Reversed.java:12)
I'm very new to programming and the teacher is very vague in his lectures any help is appreciated.
Do i need to make S a prior variable in the main method?
Re: Returning reversed string? Help please.
you're trying to call your reverseString method using the String class (well, technically not even this, but I won't go into the details). Instead, you should be calling with an actual string object.
Code Java:
String a = "hello world"; // a holds a string object
doIt(a); // call method doIt which takes a string object parameter.
doIt("this is a string literal object"); // call method doIt with a string literal, which is a string object
Another problem you have is you're code is actually found inside the reverseString method, where it should be in your main method. The reverseString method should only contain the code for reversing a string, nothing else.
Code Java:
// this code needs to be in the main method, not the reverseString method
Scanner Console = new Scanner(System.in);
System.out.println("What ever you enter will be reversed");
String str1 = Console.nextLine();
String str2 = reverseString(str1);
System.out.println(str2);
Code Java:
public String reverseString(String str)
{
// TODO: you need to put code which reverses a string here
// TODO: put a return statement which returns the reversed string you created
}
Re: Returning reversed string? Help please.
Okay so i think i understand this is what i came up wit but now i have main stock overflow?
Code Java:
public class Reversed {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String S ="This is me";
reverseString(S);
}
public static String reverseString(String S){
String str = reverseString(S);
System.out.println(str);
return S;
}
}
How do i go about changing the code so i dont get the overflow?
Re: Returning reversed string? Help please.
Re: Returning reversed string? Help please.
The latest code you posted contains an infinite loop (reverseString continuously calls itself Ad infinitum). There isn't a reason to use recursion to reverse the string (which is what it is now). Try a for loop to loop over the length of the string, adding the character value at that position to a new string which is then returned.
Re: Returning reversed string? Help please.
There's nothing wrong using recursion, but you've got to do it properly :P
But yes, I would recommend not using recursion because these algorithms are generally harder for new programmers to write.
Re: Returning reversed string? Help please.
Quote:
Originally Posted by
mindlessn00b
Hey everyone I'm missing something small I'm trying to make a code to have you put anything in you like and then the output be the reverse of that here is the code.
Code Java:
import java.util.Scanner;
public class Reversed {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Reversed.reverseString(String);
}
public static String reverseString(String S){
Scanner Console = new Scanner(System.in);
System.out.println("What ever you enter will be reversed");
String str1 = Console.nextLine();
String str2 = reverseString(str1);
System.out.println(str2);
return S;
}
}
I get this error "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
S cannot be resolved to a variable
at Reversed.main(Reversed.java:12)
I'm very new to programming and the teacher is very vague in his lectures any help is appreciated.
Do i need to make S a prior variable in the main method?
In your main method, you cannot name a variable the exact same way as a class. You can use lowercase, but you cannot tell it to return String;
Second, even if you could, you never defined String anyway. It needs to be defined somehow in the main method or else it won't work.
Also, perhaps Console has to be static.
Third, all you've done is call the method within itself. You've never reversed the String!
Fourth, even if you have, you're returning the parameter S, not the reversed String.
I'm currently baffled as to how to reverse a Sting too.