Nathan Marotta Nathan Marotta - 7 months ago 11
Java Question

Why is my program just terminating?

Program runs than terminates, why? Kind of confused because there are no syntax errors, Please explain why it terminates, thank you. fairly new to java and using arrays.

import java.text.*;

import java.util.*;

public class randomizer {
public void randomizer() throws InterruptedException
{
randomizer r = new randomizer();
int[] numbers = {3,7,2,62,1,53,16,563,12,13,75};
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int minute = rightNow.get(Calendar.MINUTE);
int seconds = rightNow.get(Calendar.SECOND);
int[] numbers2 = {10,32,61,2,5};
int[] date = {hour,minute,seconds};
int RandomNumber = (r.getRandom(date) * r.getRandom(numbers)) + r.getRandom(numbers2);
while(true) // just for test case purposes
{
Thread.sleep(1000);
System.out.println(RandomNumber);
}

}
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}


public static void main(String[] args) {
randomizer r = new randomizer();


}

}

Answer

the app is terminated and doing nothing because of this:

public static void main(String[] args) {
    randomizer r = new randomizer();
}

here you create an object of the class randomizer so far so good..

but in the code you have this:

public void randomizer() throws InterruptedException 
{....
....

}

and that is not a constructor, that is a method... so as long as you dont call it is not executed...

you need to either change that to a constructor by doing

randomizer() throws InterruptedException 
{....
....

}

or calling it in the main method

public static void main(String[] args) {
    randomizer r = new randomizer();
    r.randomizer();
}

after that modify the method because in ther you have

public void randomizer() throws InterruptedException 
{
    randomizer r = new randomizer(); //this will recursive create objects until overflows....
    int[] numbers = {3,7,2,62,1,53,16,563,12,13,75};
    Calendar rightN
Comments