Results 1 to 13 of 13
  1. #1
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Question Recursion question

    Java Code:
    public class Factorial
    {
        public static long factorial( int n )
        {
            if( n <= 1 )    
                return 1;
            else
                return n * factorial( n - 1 );
        }
    
    }
    and

    Java Code:
    import java.util.*;
    
    @SuppressWarnings("unused")
    public class FactorialTest {
    
        public static void main(String[] args) {
            
            Scanner input = new Scanner(System.in);
            
            System.out.print("Enter number: ");
            int input_number = input.nextInt();
                    
            System.out.printf("%d factorial is %l", input_number, Factorial.factorial(input_number) );
            
        }
    }

    I don't understand why when I run the code (I am using Eclipse) it gives me error message saying:

    Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'l'
    at java.util.Formatter$FormatSpecifier.conversion(For matter.java:2606)
    at java.util.Formatter$FormatSpecifier.<init>(Formatt er.java:2634)
    at java.util.Formatter.parse(Formatter.java:2480)
    at java.util.Formatter.format(Formatter.java:2414)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at FactorialTest.main(FactorialTest.java:13)


    Edit: Found it, there is no %l. But when I change System.out.printf("%d factorial is %l", input_number, Factorial.factorial(input_number) ); to %d and when I start the project it still gives me error message even though the program is working.
    Last edited by luke; 10-03-2010 at 09:10 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Perhaps you have an error in another program that is in this project?
    I have just these two files in the project. I tried to make another recursion for another problem.

    Java Code:
    public class Neper_number {
        
        [COLOR="Red"]static long factorial( int n ) {[/COLOR]
            
            if( n <= 0 )
                return 1;
            else
                return 1 + 1 / factorial(n-1);
        }
    
    }
    Eclipse says that the factorial method must return a variable of type long and gives two suggestions that make no sense (either to do the method void or to change it to long type, which is the type now).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah, I see -- yes, the error does make sense as your method isn't returning a long but rather it's returning an int.

    i.e.,
    Java Code:
       public static long factorial(int n) {
          if (n <= 1)
             return 1L; // return a long literal
          else
             return (long)(n * factorial(n - 1)); // or cast the number to long
       }
    Last edited by Fubarable; 10-04-2010 at 02:43 AM.

  5. #5
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Ah, I see -- yes, the error does make sense as your method isn't returning a long but rather it's returning an int.

    i.e.,
    Java Code:
       public static long factorial(int n) {
          if (n <= 1)
             return 1L; // return a long literal
          else
             return (long)(n * factorial(n - 1)); // or cast the number to long
       }
    I did
    Java Code:
    return 1L; // return a long literal


    and
    Java Code:
    return (long)(n * factorial(n - 1)); // or cast the number to long


    but am still getting the same error message.
    I can run the program even though the message appears but I would like to know what is causing that error.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The %l argument does not exist for Formatter class (used by printf). (See API: Formatter (Java 2 Platform SE 5.0)) As far as I know, you're best to just use %d instead. Some languages support things such as %ld but to my knowledge, Java does not.

  7. #7
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Even if I change

    Java Code:
    public static [COLOR="Red"]long[/COLOR] factorial(int n) {
          if (n <= 1)
             return 1; 
          else
             return n * factorial(n - 1); 
       }
    to int type in order to become
    Java Code:
    public static [COLOR="Red"]int[/COLOR] factorial(int n) {
    it still gives me the same message.

    Edit: I opened new project and copied/pasted the code into it. Everything seems fine. I guess something messed up with the previous project. It might be because I had a couple of .java files opened while I was actually working on two of them. Instead of opening a new project every time I was just creating a new Java class within the same project.
    Thanks to everyone who helped me out.
    Last edited by luke; 10-04-2010 at 05:47 AM.

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    ...read my post carefully.

    You are using a %l specifier instead of %d. Formatter does not recognize %l, hence the error.

  9. #9
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    ...read my post carefully.

    You are using a %l specifier instead of %d. Formatter does not recognize %l, hence the error.
    I had already changed it :)( I had edited my first post a while after posting the question because I had noticed it )

    Edit: Found it, there is no %l. But when I change System.out.printf("%d factorial is %l", input_number, Factorial.factorial(input_number) );

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by luke View Post
    Edit: Found it, there is no %l. But when I change System.out.printf("%d factorial is %l", input_number, Factorial.factorial(input_number) );
    That's not quite a sentence. What did you change it to, and what happened when you changed it?

  11. #11
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    That's not quite a sentence. What did you change it to, and what happened when you changed it?
    I've just copied some part of my first post. Look at the bottom of my first post:

    Edit: Found it, there is no %l. But when I change System.out.printf("%d factorial is %l", input_number, Factorial.factorial(input_number) ); to %d and when I start the project it still gives me error message even though the program is working.

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It must be elsewhere in your document...

    Java Code:
    package base;
    
    import java.applet.*;
    
    import java.util.*;
    
    public class base extends Applet implements Runnable {
    	{
    		Scanner input = new Scanner(System.in);
    
    		System.out.print("Enter number: ");
    		int input_number = input.nextInt();
    
    		System.out.printf("%d factorial is %d", input_number, base.factorial(input_number) );
    	}
    	public static long factorial( int n ) {
    
    		if( n <= 0 )
    			return 1;
    		else
    			return n * factorial( n - 1 );
    	}
    }
    Ignore the fact that my code is an Applet, not an Application.

    That's the exact code I'm running with no errors. Output is:
    Java Code:
    run:
    Enter number: 7
    7 factorial is 5040

    ...and no compiler nor runtime errors are provided.

  13. #13
    luke is offline Member
    Join Date
    Sep 2010
    Posts
    62
    Rep Power
    0

    Default [SOLVED] Recursion question

    Quote Originally Posted by Zack View Post
    It must be elsewhere in your document...

    Java Code:
    package base;
    
    import java.applet.*;
    
    import java.util.*;
    
    public class base extends Applet implements Runnable {
    	{
    		Scanner input = new Scanner(System.in);
    
    		System.out.print("Enter number: ");
    		int input_number = input.nextInt();
    
    		System.out.printf("%d factorial is %d", input_number, base.factorial(input_number) );
    	}
    	public static long factorial( int n ) {
    
    		if( n <= 0 )
    			return 1;
    		else
    			return n * factorial( n - 1 );
    	}
    }
    Ignore the fact that my code is an Applet, not an Application.

    That's the exact code I'm running with no errors. Output is:
    Java Code:
    run:
    Enter number: 7
    7 factorial is 5040

    ...and no compiler nor runtime errors are provided.
    Thanks again for your reply, Zack.
    I've solved this problem. I just created new project file and copied the same files. But I posted another question about recursion because obviously I don't understand very well the recursion.
    Last edited by luke; 10-04-2010 at 10:07 PM.

Similar Threads

  1. Touch recursion question
    By myst in forum New To Java
    Replies: 18
    Last Post: 06-08-2010, 06:48 PM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  3. Recursion
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 02:26 AM
  4. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 01:13 AM
  5. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 11:41 AM

Posting Permissions

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