I want to print tabs recursively but the code is generating an error.
public String Tab(int temp) {
if(temp==0)
return null;
else {
return ("\t"+Tab(temp--));
}
}
Change your line return ("\t"+Tab(temp--));
to return ("\t" + Tab(--temp));
Check
what is the difference between i++ & ++i in for loop (Java)?
if you use return ("\t"+Tab(temp--)); Tab method always will take first temp value.it will be Infinite loop. For exapmle;Tab(5); 5 5 5 5 . . .
You must be getting a stack overflow error.
return ("\t"+Tab(temp--));
The above line causes an infinite loop, as the temp
variable is reduced after the completion of the method.
The value of temp
is passed and not the reference. You should just simplify the code as
return ("\t"+Tab(temp-1));