Author Topic: Java Code help!  (Read 2446 times)

0 Members and 1 Guest are viewing this topic.

Offline M1lak0

  • Peasant
  • *
  • Posts: 129
  • Cookies: 10
    • View Profile
Java Code help!
« on: 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!

Code: [Select]
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:
Code: [Select]
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! :')
"Security is just an illusion"

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java Code help!
« Reply #1 on: 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.

Offline M1lak0

  • Peasant
  • *
  • Posts: 129
  • Cookies: 10
    • View Profile
Re: Java Code help!
« Reply #2 on: 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 ();
                                                                ^
"Security is just an illusion"

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java Code help!
« Reply #3 on: 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.
« Last Edit: August 11, 2014, 08:05:21 am by Deque »

Offline M1lak0

  • Peasant
  • *
  • Posts: 129
  • Cookies: 10
    • View Profile
Re: Java Code help!
« Reply #4 on: 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! :'(
"Security is just an illusion"

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Java Code help!
« Reply #5 on: 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.