EvilZone

Programming and Scripting => Java => : M1lak0 August 10, 2014, 04:59:39 PM

: Java Code help!
: M1lak0 August 10, 2014, 04:59:39 PM
I tried to created a java program which finds 1st 10 numbers which are divisible by 7.
I created a class and want only via recursion!

:
class div{
int a=0;
int divi()
{
if(a%7==0){
for(i=0;i<11;i++){
System.out.println("Numbers are:"+a);
a++;
divi();
}
}
else{
divi();
}
}
}
class div_main{
public static void main(String args[]){
div d1=new div();
d1.divi();
}
}

Error:
:
javac div_main.java
div_main.java:20: <identifier> expected
d1.divi();
       ^
1 error


Please help! I am learning so please don't discourage me! :')
: Re: Java Code help!
: Deque August 10, 2014, 08:02:15 PM
Geoff nailed the compiler problem. On top of it you will get a runtime error once you added a return statement: Your recursion never stops, resulting in a StackOverflowError.
: Re: Java Code help!
: M1lak0 August 11, 2014, 02:54:27 AM
ok so I used void divi() but the problem remains same! :(

Identifier expected it says, at d1.divi ();
                                                                ^
: Re: Java Code help!
: Deque August 11, 2014, 08:04:55 AM
I noticed the recursion issue as well... I assumed that the execution would just hang as opposed to cleanly exiting. I take it from your response that Java looks for this and disallows it upfront?

An error (in contrast to an exception) is no clean exit. Java doesn't check this, it can't, that would solve the halting problem. The stackoverflowerror arises, because the stack is used to save the addresses where execution goes back to, once you climb up the recursion ladder again.

@M1lak0: I have no idea what you are doing. Make sure you compiled the most recent version of your code and post your code again, please.
: Re: Java Code help!
: M1lak0 August 13, 2014, 03:49:01 PM
@M1lak0: I have no idea what you are doing. Make sure you compiled the most recent version of your code and post your code again, please.
[/quote]
So I guess its a version problem that I face this error? When I tried to compile in my college lab it resulted me in Stack Overflow as well! :( Sad!! I need to do this. Can you or any post the code for me? I tried it with recursion using a main class! :'(
: Re: Java Code help!
: Deque August 13, 2014, 05:44:10 PM
@M1lak0: I have no idea what you are doing. Make sure you compiled the most recent version of your code and post your code again, please.

So I guess its a version problem that I face this error? When I tried to compile in my college lab it resulted me in Stack Overflow as well! :( Sad!! I need to do this. Can you or any post the code for me? I tried it with recursion using a main class! :'(

We won't do the homework for you.
Either you learn how recursion works or try it without. The stackoverflow problem is related to your way using recursion.