Hi everyone I am new to the community and been programming in java for 2 weeks. This is my 3rd program so please take it easy on me, I know there are better ways to do things but this is what I know for now. The purpose of the program is to get the factorial of a number. It compiles but I never get an answer. The program is doing something because the fan on the processor starts running so the processor is doing a lot of work. Here is what I have written. I am using 2 while statements and not sure I am doing them right. Thanks for any help you can give.

//program to get factorial of a number

import java.util.Scanner;

public class Factorial
{
  //calculate factorial of a number
   public static void main( String[] args)
    {
    Scanner input=new Scanner(System.in);
     int number;//the input number
     int mutiplier;//the mutiplier
     double answer1;//the answer
     double finial;//the factorial

    //get the number to be the factorial
    System.out.print("enter any number to get it's factorial (-1 to quit):");
    number=input.nextInt();
    finial=0;
    while  (number !=-1);
     {
       mutiplier = number;
       while (mutiplier !=0);
        {
        answer1= number*mutiplier;
        mutiplier=mutiplier-1;
         finial += answer1;
         }
        System.out.printf("factorial is:%d\n",finial);
       }
         }
}
while  (number !=-1);

You never change the value of number in the loop, so this remains true forever.

Your program is stuck in an infinite loop. You said

while(number != -1)[B];[/B]

. Notice the ";". That causes the program to think "oh, the ; represents the end of the while loop". Remove that symbol and see what happens. You also never changed the value of number, so even after you make this change, it will not work. Since you never changed number, number will never be -1, and the while loop will never exit. You should change the first "while" to "if" and your program should work.

while  (number !=-1);

You never change the value of number in the loop, so this remains true forever.

Actually you are partially correct - the ";" was unintended, though, which is the cause of none of his other code being executed, which in turn is why his answer never gets printed out.

... oh, yeah, and that too. Missed the semicolon, didn't I? :)

Ok I rewrote the program and it works however I need to now modify it so the factorial class takes a number as a constructor parameter and has a method called getFactorial() that returns the factorial. Use the Factorial.java and FactorialTest.java files included as a starting point and use class Scanner to obtain the data from the user. Can someone please explain what I need to do? I don't understand.

import java.util.*;

      public class fac
      {

      public static void main(String[]args)

      {
      double x,F,P;

      System.out.println("Factorial ");

      System.out.println("Enter the number");

      Scanner sc = new Scanner(System.in);

      P=1;
      
      while (P>0)

      {   

      P = sc.nextInt();

      x = 1;

      F = 1;
      
      while (x != P)

      {

      F= F * x;

      System.out.println(F);

      x = x + 1;

      }

      F = F * x;

      System.out.println(F);

      }     

      }

      }
public class Factorial
{
    public Factorial(int number)
    {
        
    }
    
    public int getFactorial()
    {
        
    }
}
import java.util.Scanner;

public class FactorialTest
{
    public static void main( String[] args )
    {
        
    }
}

1 ) in your main method create a new Factorial object

2) put the code that reads the input from the user in the main method

3) in the Factorial constructor, store the value as a property of your object

this.number = number

4) put the code that computes the factorial in the getFactorial() method, and returns the result instead of printing it.

5) in the main method, call

System.out.println(new Factorial(input).getFactorial())

for each number the user types.

Or make it more readable by adding an id to what you print out:

System.out.println("factorial for " + input + " is " + new Factorial(input).getFactorial());

I understand what you a saying in the main. I guess where I am having the problem is in the method. The line

public Factorial(int number)

is the start of something but I am not sure what . Is this where I declare my variables or are they pulled from the main. I did this in the main

import java.util.Scanner;

public class FactorialTest
{
    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in);        
    Factorial myFactorial = new Factorial();
    System.out.println("enter the number:")
    int number = input.nextvar();
    myFactorial.Factorial(int number);
        
    }
}

I know it not done but this what I understan you are saying. In the

#
public int getFactorial()
#
{
#
 
#
}

This is were the method goes to get the factorial but instead of printing it here I end it with return.

public Factorial(int number)

That looks like the constructor for the class. Its a special method that is called one time when the class is "constructed" with a single int arg.
What you do in the constructor depends on what the class does. Minimally you would save the int arg in a class variable.

Thanks to those who have posted help. I think I am getting close to getting this to work but I am getting 3 errors when i try to compile. All three are "can not find symbol" I am sure they are stupid mistakes but I don't know what they are. Please help me to see what I am doing wrong.

import java.util.Scanner;

public class FactorialTest
{
    public static void main( String[] args )
    {
        Factorial number1 = new Factorial ();
    
        Scanner input = new Scanner (System.in);
        System.out.print("Enter the number to get factorial: (enter -1 to exit)");
        number1 = input.nextInt();
        System.out.println(number1.getFactorial());
    }
}
public class Factorial
{
    public Factorial(int number)
    {
    int x;
    int f;
    x=1;
    f=1;
    while (x!= number)
      {
      f= f * x;
      System.out.println(f);
      x=x+1;
      
      }
    number = f;
        
    }
    
    public int getFactorial()
    {
      return number;    
    }
}

getting 3 errors when i try to compile. All three are "can not find symbol"

What symbol is not found? Where is that symbol defined? Did you misspell it or are you missing code to define it?

Without seeing the full text of the error messages the question can't be answered.

Wow, where did you learn Java? I would like to learn, but don't know where to start.

commented: Please start your own thread +0

yes they are symbols I did not define or defined wrong. I am attaching a screenshoot so you can see the errors. I know the errors are simple I just am not experienced enough to know how to fix them..

Please copy and paste the full text of the error messges here, not via an attachment.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

thanks did not know how to copy from command prompt.
here is what I am getting. now it is errors becasue I made a minor change just trying thing.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\>javac Factorial.java FactorialTest.java
Factorial.java:23: cannot find symbol
symbol : variable number
location: class Factorial
return number;
^
FactorialTest.java:8: cannot find symbol
symbol : constructor Factorial()
location: class Factorial
Factorial factorial1 = new Factorial ();
^
FactorialTest.java:12: cannot find symbol
symbol : variable number1
location: class FactorialTest
number1 = input.nextInt();
^
FactorialTest.java:13: cannot find symbol
symbol : variable number1
location: class FactorialTest
System.out.println(number1.getFactorial());
^
4 errors

C:\>

cannot find symbol
symbol : variable number

Where is number defined? Compile can't find it. Same for number1

cannot find symbol
symbol : constructor Factorial()

where is the no-arg constructor for the Factorial class? The compiler can't find it.

Ok that helps!! I made some shanges getting closer now I am only getting one error and it is different. Here is the error.

C:\>Javac FactorialTest1.java Factorial1.java
Factorial1.java:4: invalid method declaration; return type required
public Factorial(int number)
^
1 error

C:\>
here are the changes I made in the code.

import java.util.Scanner;

public class FactorialTest1
{
    public static void main( String[] args )
    {    
        int number1;
        Scanner input = new Scanner (System.in);
        System.out.print("Enter the number to get factorial: (enter -1 to exit)");
        number1 = input.nextInt();
        Factorial1 factorial1 = Factorial (number1);
        
    
        
        System.out.println(factorial1.getFactorial());
    }
}
public class Factorial1
{
    public Factorial(int number)
    {
    int x;
    int f;
    x=1;
    f=1;
    while (x!= number)
      {
      f= f * x;
      System.out.println(f);
      x=x+1;
      
      }
    number = f;
        
    }
    
    public int getFactorial()
    {
      return number;    
    }
}

I am calling the method wrong but don't know what I am doing wrong.
Please help

Have you misspelled either the class name or the constructor's name. They must match.

That would be done in this line, I have them spelled the same. In the code above they are wrong but still getting same error?

Factorial factorial1 = new Factorial (number1);

What kind of editor are you using?

I have them spelled the same

What about this:

= new Factorial (number1);

vs this:

public class Factorial1

Are they spelled the same?

ok Yes they were different so I got them corrected now still have one error.
Back to
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd\.

C:\>javac Factorial1.java FactorialTest1.java
Factorial1.java:23: cannot find symbol
symbol : variable number
location: class Factorial1
return number;
^
1 error

do I need to declare number a int even though it is already in this line?

public Factorial1(int number)

the answer to that is no. Tried it and just gave me.
C:\>javac Factorial1.java FactorialTest1.java
Factorial1.java:6: number is already defined in Factorial1(int)
int number;
^
Factorial1.java:24: cannot find symbol
symbol : variable number
location: class Factorial1
return number;
^
2 errors

Where is number defined in the Factorial1 class? You need to make number a class member so that all the methods in the class can see it.

If you have changed the code, the error messages are hard to understand without seeing all of the code that goes with them.

yes I have been makeing changes. Thanks for the help but I have to go to work. I will try to work on it there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.