• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StackOverflowError

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I'm pretty new to java and this is my first post. Anyways, I've been trying to write a lottery simulator that tell how many times it takes to win the lottery, numbers which are generated from the math.random() class. This was originally a project to practice implementing arrays into my code. The code compiles and runs the loop correctly for a while, but ends in this stack overflow error. If it means anything the IDE I'm using is blueJ.

Heres my code:

I will appreciate any help with this topic anyone can offer...




 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if(numMatched != 5)
{
++timesPlayed;
readData(lotteryNums);
}




The code in your IsItAWinner(...) method calls readData(..) which in turns calls IsItAWinner given that numMatched is most probably never going to be 5 using math.random(), that is why program gets into loop there by resulting in stackoverflow.
 
Jeffrey Eanes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps I'm misunderstanding but.... is there a way for this loop to just run until it terminates, ie. (numMatched == 5)? Can you give me a better understanding of what a StackOverflow is?

thanks!
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeffrey Eanes wrote:Perhaps I'm misunderstanding but.... is there a way for this loop to just run until it terminates, ie. (numMatched == 5)? Can you give me a better understanding of what a StackOverflow is?

thanks!



StackOverflowError is Thrown when a stack overflow occurs because an application recurses too deeply.



Instead of checking for numMatched , check for timesPlayed as in the above.
 
Jeffrey Eanes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed my checking to timesPlayed, and fixed some of the code to calculate the number times played. The loop runs 5,791 times before giving me a stackoverflow error. I really just want to know if there is a way to make it run until it reaches (numMatched == 5), or if that is impossible. Here's the new code if that helps. Thank you for all your help so far I really appreciate it.

New code:


import java.util.*;
public class LotteryArrays
{
public static void IsItAWinner(int[] lottoTicket, int[] lotteryNums, int timesPlayed) //Evaluates user and lottery numbers
{
int numMatched = 0;
for(int f=0;f<5;f++) //Finds the matching numbers
{
if(lottoTicket[f] == lotteryNums[f])
++numMatched;
}
System.out.println("User Numbers: "); //Displays results
for(int i=0;i<5;i++)
System.out.print(lottoTicket[i]);
System.out.println();
System.out.println("Lottery Numbers: ");
for(int i=0;i<5;i++)
System.out.print(lotteryNums[i]);
System.out.println();
System.out.println("Numbers Correct: " + numMatched);
if(numMatched == 5)
{
System.out.println("You are the GRAND PRIZE WINNER!");
System.out.println("You played " + timesPlayed + " to win the lottery");
}
if(timesPlayed != -1)
{
timesPlayed++;
System.out.println("You played " + timesPlayed + " times");
readData(lotteryNums, timesPlayed);
}
}
public static void readData(int[]lotteryNums, int timesPlayed) //Gets the users' pick for the lottery
{
int[] lottoTicket = new int[5];
for(int i=0;i<lottoTicket.length;i++)
lottoTicket[i] = (int)(Math.random()*10);
IsItAWinner(lottoTicket, lotteryNums, timesPlayed); //calls method to evaluate the lottery numbers and user numbers
}
public static void main(String[] args)
{
int timesPlayed = 0;
int[] lotteryNums = new int[5];
for(int j=0;j<lotteryNums.length;j++)
lotteryNums[j] = (int)(Math.random()*10); //Generates lottery numbers
System.out.println("Choose your five lottery numbers:");
readData(lotteryNums, timesPlayed); //calls methods to pick numbers and analyze arrays
System.out.println("Thanks for playing!");
}
}
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell me when timesPlayed is going to be -1? It will never be -1 and eventually program will exit with stackoverflow error. You have to specify how many times game can be played.

f(timesPlayed != -1)
{
timesPlayed++;
System.out.println("You played " + timesPlayed + " times");
readData(lotteryNums, timesPlayed);
}
}



It should be something like the following where I have specified the end condition for the program.


 
Jeffrey Eanes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that timesPlayed would never be -1, but if numMatched == 5, wouldn't the program terminate? also I wrote it with an end condition it like this:

if(timesPlayed < 1000000)
{
timesPlayed++;
System.out.println("You played " + timesPlayed + " times");
readData(lotteryNums, timesPlayed);
}

this also ended in an error after something like 5500 times. The whole point I'm trying to find out is how many times the loop would have to go through itself in order for numMatched to be equal to five, thus terminating the program. And thanks I didn't realize that it had to be a specified end condition.
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately You only have a finite no of stacks that you can have in a recursive program. Once JVM hits around 5550, it runs out of stacks for your program and exits by throwing StackOverflowError.




 
Jeffrey Eanes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's the answer I was looking for! I suppose that clears things up though it doesn't really solve the problem unfortunately... Anyways, I appreciate your help!
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can change recursion approach to iteration approach in your program.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I am concerned, there is something wrong when all the methods are tagged static. It suggests to me that you are not using object-oriented programming. Search this forum and “Java™ in General” because there were abotu two threads about static versus non-static. About three weeks ago.
I have shortened some lines and added code tags to your first post and you can see how much better it is to read. And welcome to the Ranch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic