-1

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();


}

}
  • 1
    format you code correctly per java standards, `UpperCamelCase` class names, `lowerCamelCase` method and variable names. –  May 05 '16 at 18:29
  • What are you programming in? If you're using any IDE for programming, it will usually have an error console. If you run the program and it exits, it should give you errors. – Ethan Moore May 05 '16 at 18:30
  • 6
    If you meant for `public void randomizer()` to be a constructor, once you fix it, it will cause a `StackOverflowError` right away. – Sotirios Delimanolis May 05 '16 at 18:30
  • After catching `InterruptedException`, that is. – rgettman May 05 '16 at 18:31
  • using eclipse, no error, it just runs than terminates with nothing shown @EthanMoore – Nathan Marotta May 05 '16 at 18:31
  • debug the app and see where is the issue... – ΦXocę 웃 Пepeúpa ツ May 05 '16 at 18:33
  • Yeah, then your main string probably isn't knowing what to call, because your format is wrong. Look at @JarrodRoberson 's answer. – Ethan Moore May 05 '16 at 18:34
  • @NathanMarotta put a breakpoint on the first line of the randomizer() method and see if it gets called when you run in Debug – Kevin Hooke May 05 '16 at 18:34
  • thank you for all the input @ΦXoce웃Пepeúpa – Nathan Marotta May 05 '16 at 18:34
  • See this very similar question: http://stackoverflow.com/q/17250137/10077 – Fred Larson May 05 '16 at 18:47
  • Please read [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. An excessive number of poorly received questions that are off-topic will get you banned from asking questions, and you do not want that do you? –  May 05 '16 at 18:52

3 Answers3

3

Your randomizer class has no constructor defined, so constructing it in your void main method does nothing.

Of note, your class does have a void randomizer() method defined - this is probably a bug. Note, classes should be Pascal case (Randomizer and methods should be camel case (randomizer).

Darth Android
  • 3,437
  • 18
  • 19
  • so should i change it to return an int? as the return type – Nathan Marotta May 05 '16 at 18:30
  • 2
    Using a debugger in an IDE and stepping through the code would have made this pretty obvious. The debugger is your friend, learn to use one! – Kevin Hooke May 05 '16 at 18:31
  • Take a read of the Java Tutorial section on Constructors : https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – Kevin Hooke May 05 '16 at 18:32
  • 1
    All questions are good questions, but the trouble with getting started here on SO you are right that privileges on the site are earned and awarded from the community itself. When you've built up your experience try answering questions from other users too, and you'll build up some rep on the site in no time. Have fun and good luck with your programming :-) – Kevin Hooke May 05 '16 at 18:39
  • 2
    This has nothing to do with being nice, @NathanMarotta. We have standards. You can take the tour and read the help center to understand what those are. – Sotirios Delimanolis May 05 '16 at 18:40
  • 1
    **Be Responsible:** Please read [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. An excessive number of poorly received questions that are off-topic will get you banned from asking questions, and you do not want that do you? –  May 05 '16 at 18:42
  • Yeah I know that but you don't have to down vote my questions, just simply tell me, because now I'm not even allowed to ask questions. @SotiriosDemlimanolis – Nathan Marotta May 05 '16 at 18:44
  • I feel as if my questions do follow SO guidelines. @JarrodRoberson – Nathan Marotta May 05 '16 at 18:45
  • @NathanMarotta - then you have not read the guidelines for comprehension, because they do not. The consensus of the community agrees. Reviewing all your previous questions proves that none of them even try to adhere to the most basic guidelines in the links in my comment above. –  May 05 '16 at 18:49
0

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
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Thank you for explaining, it makes complete sense now, I now understand. Many thanks. – Nathan Marotta May 05 '16 at 18:47
  • I didn't really think of that I thought that every time you make a new object like MethodName method = new MethodName(); I thought that code automatically runs the code since you instiated it. – Nathan Marotta May 05 '16 at 18:51
0

You ve created a randomizer object but didnt actually do anything else. The method you implement (randomizer()) is not constructor so you need to invoke it. So inside your main method you need to do those 2 things right now:

randomizer r = new randomizer(); r.randomizer();

in order for your logic inside class to start.

PS1 read the naming conventions in java PS2 it looks like you want to implement a constructor. if your method 'public void randomizer() ' was supposed to be one then remove the keyword (returning type) 'void' and when you use the 'randomizer r = new randomizer();' inside main, then your logic will execute..