This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes StackOverflowError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StackOverflowError" Watch "StackOverflowError" New topic
Author

StackOverflowError

Jeffrey Eanes
Greenhorn

Joined: Mar 31, 2012
Posts: 5
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...




Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
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

Joined: Mar 31, 2012
Posts: 5
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

Joined: Jan 22, 2007
Posts: 196
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

Joined: Mar 31, 2012
Posts: 5
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

Joined: Jan 22, 2007
Posts: 196
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

Joined: Mar 31, 2012
Posts: 5
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

Joined: Jan 22, 2007
Posts: 196
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

Joined: Mar 31, 2012
Posts: 5
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

Joined: Jan 22, 2007
Posts: 196
You can change recursion approach to iteration approach in your program.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StackOverflowError
 
Similar Threads
Trouble with lottery
Extracting numbers from from a user input and comparing it to
Battleships - guys please help me out!
Using GridLayout as GUI
Creating simple GUI interface for BattleShips