Results 1 to 4 of 4

Thread: Recursion

  1. #1
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Recursion

    Driver Class
    Java Code:
    public class Main {
    	
    	public static void main(String[] args) {
    	
    		Recursion_Palindromes rp = new Recursion_Palindromes();
    		rp.input();
    		System.out.println(rp.reverse(input));
    		
    	}
    }
    Recursion Palindrome Class
    Java Code:
    import java.util.Scanner;
    
    public class Recursion_Palindromes {
    
    	static String input = "";
    
    	static void input() {
    		System.out.println("Input a string"); //prompts user to enter a string
    		Scanner scan = new Scanner(System.in); //reads user input   
    		input = scan.nextLine(); // stores user input
    	}
    
    	public static String reverse(String input) {
    		if(input.length() == 1)
    			return (reverse(input));
    		else
    			return(reverse(input.substring(1)) + input.charAt(0));
    	}
    
    }
    Console:
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	input cannot be resolved to a variable
    
    	at Main.main(Main.java:7)


    How would I fix this simple error?
    And what is the source of the error?
    Last edited by Dinomite07; 04-27-2016 at 01:35 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,425
    Rep Power
    9

    Default Re: Recursion

    At line 7, prefix the variable "input" with its class name.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,918
    Rep Power
    22

    Default Re: Recursion

    Or don't bother passing it in in the first place as you already have it in the class.
    And you can get rid of all the 'static'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,312
    Blog Entries
    7
    Rep Power
    24

    Default Re: Recursion

    At line #15 of your second class you should simple return 'input' (don't try to reverse it), because now your method will run ad nauseam ... also line #17 doesn't make sense.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Recursion
    By allaudin in forum Forum Lobby
    Replies: 2
    Last Post: 07-21-2013, 04:59 PM
  2. Recursion Help!
    By raffs03 in forum New To Java
    Replies: 5
    Last Post: 06-24-2013, 09:08 AM
  3. Need help with recursion
    By Playboytrey in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 01:36 AM
  4. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  5. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •