Results 1 to 5 of 5
  1. #1
    TripleZigZag is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Cannot find symbol

    I'm new to Java but I do have some programming experience. I'm working on a piece of code that accepts an integer from the user, and spits out its factorial.
    But I keep getting a "Cannot find symbol" error at:
    result = Integer.toString(facter);

    I can't for the life of me figure out whats going on. Any help is appreciated. Thanks guys and have a good day. Code is below.

    Java Code:
    	int count = 0;
    	String result = "";
    	Scanner keyboard = new Scanner(System.in);
        	int userinput = keyboard.nextInt();
    
        	if (userinput <= 0) {
        		System.out.println("Please enter a number larger than 0");
        	} else {
        		while (count < userinput) {
        		        int facter = userinput * (userinput-1);
        			userinput = (userinput-1);
        			count = count-1;
        		}
    		result = Integer.toString(facter);
        		System.out.println(result);
        	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    The variable facter is out of scope once you've exited the while loop. Define it within the scope of where you wish to use it (eg outside the while loop) and it will compile (whether this results in the behavior you want is up to you)

  3. #3
    TripleZigZag is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    The variable facter is out of scope once you've exited the while loop. Define it within the scope of where you wish to use it (eg outside the while loop) and it will compile (whether this results in the behavior you want is up to you)
    Figured as much, but I can't think of any other way to write this program. Hints?

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Declare it after you declare userinput.

    Your loop will run endlessly, because userinput and count are both being decremented at the same rate, and if the condition is true to begin with it will never stop being true. (Edit: Actually it'll stop when it goes out of the range of an int, but that won't provide the result you're looking for either.)

    Also, look up the ++ and -- operators; "count--" is a lot quicker to write than "count = count - 1".
    Last edited by Iron Lion; 02-01-2011 at 09:27 PM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    it might be simpler to create a method which determines the factorial of some number and returns an int
    Java Code:
    public static int fact(int n)
    Then simply get input, call this method with the input and print the answer.

    Another thing which could be easier is to simply use a for loop
    Java Code:
    for(int i = 1; i <= input; i++){
      //do something
    }
    If you like a while loop try just doing
    Java Code:
    while(input > 0){
      //do something
    }

Similar Threads

  1. Cannot find symbol
    By Johanis in forum New To Java
    Replies: 18
    Last Post: 11-09-2010, 08:34 PM
  2. Still cannot find symbol!
    By Johanis in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 04:32 PM
  3. Can not find symbol ???
    By AliceNewbie in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 01:44 AM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 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
  •