package com.codegym.task.task04.task0427;
/*
Describing numbers
*/
import java.io.*;
import java.util.Scanner;
public class Solution {
static Scanner scan = new Scanner(System.in);
static int number = scan.nextInt();
Solution solu = new Solution();
int newNumber() {
number %= number;
return number;
}
int number1 = newNumber();
public static void main(String[] args) throws Exception {
System.out.println(number);//write your code here
Solution solut = new Solution();
if (number < 10 && solut.number1 == 0)
System.out.println("even single-digit number");
else {
if (number >= 10 && number < 100 && solut.number1 == 0)
System.out.println("even two-digit number");
if (number >= 100 && number < 1000 && solut.number1 == 0)
System.out.println("even three-digit number");
if (number < 10 && solut.number1 == 1)
System.out.println("odd single-digit number");
if (number >= 10 && number < 100 && solut.number1 == 1)
System.out.println("odd two-digit number");
if (number >= 100 && number < 1000 && solut.number1 == 1)
System.out.println("odd three-digit number");
}
}
}
Was trying an out of the box solution, and I got: Exception in thread "main" java.lang.StackOverflowError, could someone explain why?
Archived
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
29 November 2019, 01:39
static Scanner scan = new Scanner(System.int):
static int number = scan.nextInt();
Solution solu = new Solution();
This line is an infinite loop that will continue making solution objects until the computer's memory is full and throw a stack overfolow error. I would reset the task and only write your code where it says to, only inside the main method. You do not need to create a Solution object to solve this as the main method is static.
0
yehuda b
30 November 2019, 23:00
Thanks so much for answering, are you essentially meaning that creating and object w/o doing anything with it just keeps on repeating? (I thought after posting the question that the error was because I created an object for naught)
Thanks again, this dialogue advances me a lot and most importantly gives importance to the material making it feel more real (a doctor could tell his friends "I had a hard day with 12 patients coming down with the flu etc. etc. while what should an aspiring developer say " I learned method overloading/passing args into parameters today..." what's the guy talking about??
0
Guadalupe Gagnon
1 December 2019, 03:32
No, I mean specifically that line is run every time a Solution object is created. Because that line creates a new Solution object, that the new Solution object will have that line and create another, which will have that line and create another, and that will have that line and create another, and etc etc etc. to infinity (or more precisely memory runs out).
0
yehuda b
1 December 2019, 18:50
Got it, Thanks!!
0