Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Returning reversed string? Help please.

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    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.

    // 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);

    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
    }
    Last edited by helloworld922; October 17th, 2010 at 04:05 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?
    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?
    Last edited by mindlessn00b; October 17th, 2010 at 05:03 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Returning reversed string? Help please.

    anyone got any idea?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Returning reversed string? Help please.

    There's nothing wrong using recursion, but you've got to do it properly

    But yes, I would recommend not using recursion because these algorithms are generally harder for new programmers to write.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Returning reversed string? Help please.

    Quote Originally Posted by mindlessn00b View Post
    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.
    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.

Similar Threads

  1. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  2. url input stream returning blank
    By yo99 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: June 2nd, 2010, 08:14 PM
  3. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  4. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM