Ask coding questions

← Back to all posts
1
Java program ends before printing all the results
leeappelbaum (0)

I am running a simulation that prints results after each iteration. If I run 100 iterations, I may see anywhere from 40 to 75 results printed to the output window - it's different every time. When I copied-pasted into BlueJ, all the output was printed. I am running on a Macbook Pro under Chrome, Safari and Firefox, all with the same results. Adding a loop at the end to print one million nulls succeeds in writing the real data, but I would like to avoid such a kludge. Thank you.

Answered by SPQR (325) [earned 5 cycles]
View Answer
Commentshotnewtop
1
SPQR (325)

Perhaps delay the end of the program, with a sleep function or something similar. I've noticed a similar issue with C++ as well, and I think it has something to do with the program ending before the JavaScript has displayed the output in the browser window.

1
leeappelbaum (0)

@SPQR
Thank you. I slept for a couple of seconds and now get my output. Definitely cleaner that printing nulls.
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
}

1
SPQR (325)

@leeappelbaum Glad I could help :) If I solved your problem, could you mark my original comment as the answer? It would be greatly appreciated. :D

1
JustARatherRidi (185)

Could you share a link to the repl?

1
leeappelbaum (0)

@JustARatherRidi
https://repl.it/@leeappelbaum/Monte-Hall
Oops - I shared it but forgot to include the link. The purpose of the program is to demonstrate to high school math students that the Monte Hall problem result we get using math plays out in "real life."

1
JustARatherRidi (185)

@leeappelbaum That's odd, mine seems to work right every time.

What Macbook pro are you using?

1
leeappelbaum (0)

@JustARatherRidi
MacBook Pro (Retina, 15-inch, Mid 2015)
2.2 GHz Intel Core i7
Running 10.11.6
I updated my script - are you running a version with either the one million prints or the sleep at the end of the Main module?

1
JustARatherRidi (185)

@leeappelbaum I tried the million prints, the sleep, both of them, and neither of them, everything seems to work perfectly. And your specs seem more than enough to handle something like this, so you might want to put this one on https://repl.it/bugs/