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: variable fact might not have been initialized

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default variable fact might not have been initialized

    The frst part of the program is working perfectly fine . The class useTest is linked to another class, Test , which is working out the first part of the program. I have an error in the bottom part of the program , can anyone help please ? thanks


    import java.util.* ;

    public class useTest {
    public static void main ( String args[]) {
    Scanner kb = new Scanner ( System.in) ;
    System.out.print ( "\fPlease enter degrees : " ) ;
    int d = kb.nextInt () ;
    double r = (double)Test.toRadians (d) ;

    System.out.println ( " Degrees is : " + d + " and radians is : " + r ) ;
    }

    public static int fact ( int d, double r ){
    int fact , n , c = 1;
    System.out.println("Enter an integer to calculate it's factorial");
    Scanner kb = new Scanner(System.in);

    n =kb.nextInt () ;

    if ( n < 0 )
    System.out.println("Number should be non-negative.");
    else
    {

    for ( c = 1 ; c <= n ; c++ )
    fact = 1*c ;

    System.out.println("Factorial of "+n+" is = "+ fact); // ERROR IS HERE //
    }

    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: variable fact might not have been initialized

    When posting code, please use the highlight tags to preserve formatting.

    What happens if c starts out as greater than n? That might not be logically possible (actually it is if the user enters a 0), but the compiler has no way of knowing that, so it's telling you that there's a chance the fact variable was not initialized before you're trying to print it out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    davidjava (March 11th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: variable fact might not have been initialized

    Thanks for your suggestion . An error still pops out however , import java.util.* ;

    public class useTest {
    public static void main ( String args[]) {
    Scanner kb = new Scanner ( System.in) ;
    System.out.print ( "\fPlease enter degrees : " ) ;
    int d = kb.nextInt () ;
    double r = (double)Test.toRadians (d) ;

    System.out.println ( " Degrees is : " + d + " and radians is : " + r ) ;
    }

    public static int fact ( int d, double r ){
    int fact = 0 ;
    int c = 1;
    int n = 0;
    System.out.println("Enter an integer to calculate it's factorial");
    Scanner kb = new Scanner(System.in);

    n =kb.nextInt () ;

    if ( n < 0 )
    System.out.println("Number should be non-negative.");
    else
    {

    for ( c = 1 ; c <= n ; c++ )
    fact = 1*c ;

    System.out.println("Factorial of "+n+" is = "+ fact);
    }

    }
    }

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: variable fact might not have been initialized

    Like I said, please use the highlight tags. Also please copy and paste the full text of the error.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. variable might not have been initialized!!
    By bassie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2012, 12:39 PM
  2. variable might not have been initialized...help appreciated
    By neontiger in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 05:58 PM
  3. Variable might not have been initialized
    By JavaGirl9001 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2011, 11:24 AM
  4. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  5. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM