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 4 of 4

Thread: Whats wrong with my code?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Whats wrong with my code?

    I am writing a java algorithm to find the factorial of a number (for example the factorial of 5 is 5*4*3*2*1 or 120), I know I can use recursion but I am already have a code that uses such a style and would like to develop another piece of code that does not. The code below involves user input. The code's objective is to take a user input, which is a positive into its scanner and run it through the for loop. The for loop takes the user's variable multiplies it to 1 the first time then takes that results and multiplies that result with the second run and so and so fourth until the user's variable reaches 1. However, the result when run on eclipse is nothing, it terminates after inputting any number and does not give the specified error if I type in a negative number. There is no output at all. Can someone please suggest a solution for this problem?

     
    import java.util.Scanner;
     
    public class factorial {
     
    	private Scanner factint;
     
    	public void calfact()
    	{
    		int x; 
    		factint = new Scanner(System.in);
    		System.out.println("Enter a positive number you would like to take the factorial of");
    		for(x= factint.nextInt(); x ==1 ;)
    		{
    			if(x < 0)
    				System.err.println("Error: Invalid Declaration for Input Variable");
    			int newresult = 1;
    			newresult= newresult * x;
    			System.out.println(newresult);
    		}	
    	}
     
    	public static void main(String[] args) 
    	{
    		factorial fobj = new factorial();
    		fobj.calfact();
    	}
     
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Whats wrong with my code?

    Quote Originally Posted by patelvrajn View Post
    		for(x= factint.nextInt(); x ==1 ;)
    This has very little sense. Firstly, it's better to have the call to nextInt out (before) the for. Secondly, the condition is wrong.
    But also the body of the for is wrong.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Whats wrong with my code?

    check out this code

    ....
    Last edited by copeg; December 15th, 2013 at 09:54 AM. Reason: Removed spoonfed code

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Whats wrong with my code?

    @sam965, please read the forum guidelines and the following:
    The Problem with Spoon-feeding

Similar Threads

  1. Whats wrong in my code?
    By kalaicse30@gmail.com in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 18th, 2013, 11:44 PM
  2. [SOLVED] Im not sure whats wrong with my code
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 25th, 2013, 09:08 PM
  3. help me with a code, whats wrong?
    By Heizzer10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 17th, 2012, 11:30 AM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 04:34 PM