PDA

View Full Version : this little wanker won't compile...


inkedmn
06-11-2002, 12:42 PM
here's the code...


public class Recurse {
public static void main(String [] args) {
String x = (args[0]);
int y = Integer.parseInt(x);
System.out.println("Using recursion: " + doRecursionFactorial (y));
System.out.println("Using a for loop: " + doLoopFactorial (y));
}
public static int doRecursionFactorial(int n) {
int result = n;
if (n > 1) {
return n *= doRecursionFactorial(n - 1);
} else {
return result;
}
}
public static int doLoopFactorial(int q) {
int number = q;
int result = 1;
if (number > 1) {
for (int x = 1; x <= number; x++); {
result = (result * x);
}
} else {
return result;
}
}
}


and here's the compile error i get...


C:\dev_practice\myjava>javac Recurse.java
Recurse.java:21: cannot resolve symbol
symbol : variable x
location: class Recurse
result = (result * x);
^


i'm stumped (but i'm also pretty new:))

[edit]
the arrow symbol is pointing to the 'x', but the bb jacks up the formatting

kmj
06-11-2002, 12:57 PM
I suggest for sh*ts and giggles you change the variable x in that function to something else; try "index". I don't see what's wrong with your code, though.


Never mind. I see what's wrong.
Look hard at your for loop, then smack yourself on the forehead.

Strike
06-11-2002, 01:17 PM
Code looks okay to me too. Maybe, just for fun you can declare x up with the other ints and just piss all over scoping :)

scanez
06-11-2002, 01:29 PM
You have a semicolon after your for loop before the line in question, hence x is not declared when it is used. Remove the semicolon and all should be good ;)

kmj
06-11-2002, 01:42 PM
a gift: :)

public static int doLoopFactorial(int number) {
int result = 1;

if (number < 1)
number = 1;

for (int x = 2; x <= number; x++)
result = (result * x);

return result;
}


carp; forgot the closing tag

edit 2: what's up with vbulletin adding all those extra lines in the code tags? or is that another descrepency between ie and every other browser?

Strike
06-11-2002, 03:36 PM
kmj: nope, does it in every browser I've viewed this board in (quite a few)

kmj
06-11-2002, 03:39 PM
Originally posted by Strike
kmj: nope, does it in every browser I've viewed this board in (quite a few)

the code looks fine in IE 5.5.


oh, and I just tested that code. It seems to work. :)

Strike
06-11-2002, 03:52 PM
Ah, true, I thought it did it in IE as well, but it seems okay in IE6 (what I'm using right now). Remind me to hate vBulletin now too :)

inkedmn
06-11-2002, 04:10 PM
ok, got it all working (thanks for all the help amigos :)):

and, i added a test for the correct number of arguments:


public class Recurse {
public static void main(String [] args) {
if (args.length == 1) {
String s = (args[0]);
int y = Integer.parseInt(s);
System.out.println("Using recursion: " + doRecursionFactorial (y));
System.out.println("Using a for loop: " + doLoopFactorial (y));
} else {
System.out.println("Usage: Recurse {an integer}");
return;
}
}
public static int doRecursionFactorial(int n) {
int result = n;
if (n > 1) {
return n *= doRecursionFactorial(n - 1);
} else {
return result;
}
}
public static int doLoopFactorial(int q) {
int number = q;
int result = 1;
if (number > 1) {
for (int x = 1; x <= number; x++) {
result = (result * x);
} return result;
} else {
return result;
}
}
}


:)

now all i need is some exception handling incase it gets passed a non-integer...

inkedmn
06-11-2002, 04:58 PM
ok, don't hate me for this, but i added a few things to the code and it works correctly.

i'd just like for you java guys to take a look at it and tell me if it's coded WELL...

thanks (i'll just link to it to save space)

http://www.inkedmn.net/code/Recurse.java

thanks :)

[edit]

and if you don't want to dl it, here's a text version:

http://inkedmn.net/code/Recurse.txt