Results 1 to 5 of 5
Thread: Cannot find symbol
- 02-01-2011, 08:21 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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); }
- 02-01-2011, 08:26 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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)
- 02-01-2011, 08:29 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
- 02-01-2011, 09:25 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 02-02-2011, 02:36 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
it might be simpler to create a method which determines the factorial of some number and returns an int
Then simply get input, call this method with the input and print the answer.Java Code:public static int fact(int n)
Another thing which could be easier is to simply use a for loop
If you like a while loop try just doingJava Code:for(int i = 1; i <= input; i++){ //do something }
Java Code:while(input > 0){ //do something }
Similar Threads
-
Cannot find symbol
By Johanis in forum New To JavaReplies: 18Last Post: 11-09-2010, 08:34 PM -
Still cannot find symbol!
By Johanis in forum New To JavaReplies: 1Last Post: 11-04-2010, 04:32 PM -
Can not find symbol ???
By AliceNewbie in forum New To JavaReplies: 1Last Post: 02-17-2010, 01:44 AM -
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 08:26 PM -
cannot find symbol symbol : class Item location: package platypos.services.order
By officialhopsof in forum New To JavaReplies: 3Last Post: 05-01-2008, 08:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks