help..my loop wont stop!

im making a basic "casino" program. When i try my slots game, I want it to go back to the main menu when the user presses no. But for some reason, the popup box keeps looping while in play and I don't know how to stop it. (I think the bracket structure is causing this malfunction) If any one can find the problem, it would be most appreciated.

import javax.swing.*;

import java.util.*;

import java.io.*;

OVER AND OVER AGAIN!!

publicclass Casino{

staticfinalint ARRAY_SIZE = 100;

static String [] usernames =new String[ARRAY_SIZE];

static String [] passwords =new String[ARRAY_SIZE];

staticint [] balence =newint[ARRAY_SIZE];

publicstaticvoid main(String [] args)throws IOException{

String userString ="";

String passString ="";

int balenceInt = 0;

//Select the file, and open the connection to it

JFileChooser jfc =new JFileChooser();

jfc.showOpenDialog(null);// opens the chooser

String file = jfc.getSelectedFile().getAbsolutePath();

Scanner sc =new Scanner(new File(file));

// read one line and store each element in the array

char [] encryption =newchar[26];

String encString = sc.nextLine();

for(int i = 0; i < encryption.length; i++){

encryption[i] = encString.charAt(i);

}

// I made an int variable called num to keep track of usernames and passwords

int num = 0;

while(sc.hasNext()){

// read and store username

userString = sc.nextLine();

usernames[num] = userString;

// read and store passwords

passString = sc.nextLine();

String decryption ="";

for (int i = 0; i < passString.length(); i++){

char c = passString.charAt(i);

// decryted pwords process

int position = -1;

for (int j = 0; j < encString.length(); j++){

if ( encryption[j] == c){

position = j;

}

}

position = position - passString.length() + 96;

if (position < 97){

position = position + 26;

}

decryption += (char)position;

}

passwords[num] = decryption;

// Read and store balence.

balenceInt= Integer.parseInt(sc.nextLine());

balence[num] = balenceInt;

num++;

}

sc.close();

JOptionPane.showMessageDialog(null,"Hello and Welcome to Java Gaming Corporation");

String [] loginValues ={"Customer","Adminstration","Quit"};

String loginMessage ="What type of user are you?";

int selection = JOptionPane.showOptionDialog(null, loginMessage,"Security Central",

0, JOptionPane.QUESTION_MESSAGE, null, loginValues, loginValues[0]);

//I used a while loop to go back to the main button menu.

while (selection!= 2){

// If user clicks existing user...go to multiple input boxes!

if (selection ==0){

JTextField idField =new JTextField();

JTextField passwordField =new JPasswordField();

Object[] inputFields =new Object [4];

inputFields[0] ="Id:";

inputFields[1] = idField;

inputFields[2] ="password:";

inputFields[3] = passwordField;

JOptionPane.showConfirmDialog(null, inputFields,"Login", JOptionPane.DEFAULT_OPTION,

JOptionPane.QUESTION_MESSAGE);

String tempId = ((JTextField) inputFields [1]).getText();

String tempPassword = ((JTextField) inputFields[3]).getText();

// Since pwords and usernames are related, the scanner will find

//where the username is

int index = -1;

for(int i=0; i<usernames.length; i++){

if(tempId.equals(usernames[i])){

index = i;

}

}

// If the username is not there, it won't work..no username found

if(index == -1){

JOptionPane.showMessageDialog(null,"Invalid Username");

}else{

// once the username is found, the scanner must use the index

// to find pword in the array in the same index value. if the pword value

// is not equal to user inputted pword, invalid pword

if (tempPassword.equals(passwords[index])){

balence[index] += 10;

JOptionPane.showMessageDialog(null,"Login Successful, Hello!");

String [] customerValues ={"PLAY NOW","Options","Quit"};

String customerMessage ="What would you like to do " + usernames[index] +"?";

int customerSelection = JOptionPane.showOptionDialog(null, customerMessage,"Security Central",

0, JOptionPane.QUESTION_MESSAGE, null, customerValues, customerValues[0]);

while (customerSelection != 2){

if (customerSelection ==0){

String [] playValues ={"SLOTS","DOUBLE OR NOTHING","LOTTERY","BACK TO MAIN MENU"};

String playMessage ="What game would you like to play " + usernames[index] +"?";

int playSelection = JOptionPane.showOptionDialog(null, playMessage,"USER OPTIONS",

0, JOptionPane.QUESTION_MESSAGE, null, playValues, playValues[0]);

while (playSelection != 3){

if (playSelection ==0){

String [] slotValues ={"YES","NO"};

String slotMessage ="This game costs 100 Points. Do you want to play?";

int slotSelection = JOptionPane.showOptionDialog(null, slotMessage,"SLOTS",

0, JOptionPane.QUESTION_MESSAGE, null, slotValues, slotValues[0]);

while (slotSelection != 1){

if (slotSelection ==0){

balence[index] -= 100;

int num1 = 1 + (int) (Math.random()*3);

int num2 = 1 + (int) (Math.random()*3);

int num3 = 1 + (int) (Math.random()*3);

if (num1 == num2 && num1 == num3){

balence[index] += num1 * 300;

JOptionPane.showMessageDialog (null,"CONGRATULATIONS! YOU WIN! Your numbers are:" + num1 +" / " + num2 +" / " + num3 +"\n You win" + num1*300);

}else{

JOptionPane.showMessageDialog (null,"Sorry. You didn't win. Better luck next time! \n Your numbers: "+ num1 +" / " + num2 +" / " + num3);

}

}

}

}

}

}

}

}

}

}

}

}

}

Message was edited by:

antonio_montana>

[11330 byte] By [antonio_montanaa] at [2007-11-26 15:52:22]
# 1
Put in some debugging code to see where the loop is going. Check the loop conditions especially.Decompose your code into multiple methods and/or classes. It's easier to read, understand, and debug clean modular code.
paulcwa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 2
okay.i try that but im just curious..is my pop up boxes right. i put a while loop for each "quit" button..cause i want it to loop back to main menus once its done its operation..
antonio_montanaa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 3
The previous poster may have been hinting that no one is going to read a method that goes on and on like your main!
DrLaszloJamfa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 4
> while (selection!= 2) {> // code snipped> }There's nothing in that loop which ever ever ever changes the value of selection, so if it isn't equal to 2 the first time, guess what? It's never equal to 2, so you'll loop forever.
warnerjaa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 5

> > while (selection!= 2) {

> > // code snipped

> > }

>

> There's nothing in that loop which ever ever ever changes the value of

> selection, so if it isn't equal to 2 the first time, guess what? It's never

> equal to 2, so you'll loop forever.

Similarly for the other while-loops.

KelVarnsona at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 6

JOptionPane.showMessageDialog (null,"Sorry. You didn't win. Better luck next time! \n Your numbers: "+ num1 + " / " + num2 + " / " + num3);

}

}

}

}

}

}

}

}

}

}

}

}

}

Surely this is some sort of record!

cotton.ma at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 7
oh okay i understand wat you're saying, so do i have to put equal two..the reason i made not equal 2 is because i want my program to go back to the main menu..so what would the solution be here..(besides the use of methods) cause i only have 3-4 days left to summit this..
antonio_montanaa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 8
lmao cotton.m im already embarassed..im gonna cry lol..im such a noob at java..Message was edited by: antonio_montana (the noob)
antonio_montanaa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 9

> Surely this is some sort of record!

At least his braces are on separate lines. I've seen the following (longer than four braces, too):

{

{

{

{

}}}} // close all in one line!

Made it extremely hard to read.

doremifasollatidoa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 10

> lmao cotton.m im already embarassed..im gonna cry

No don't cry. I am sort of impressed it compiles.

Just treat it as a learning experience. If you have 13 closing } in a row it's a sign from the Java gods that your code needs refactoring (needs to be broken down into smaller methods).

cotton.ma at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 11

> [code]JOptionPane.showMessageDialog (null,"Sorry. You

> didn't win. Better luck next time! \n Your numbers:

> "+ num1 + " / " + num2 + " / " + num3);

> }

>

>}

>

> }

>

> }

>

>}

>

> }

>}

> /code]

>

> Surely this is some sort of record!

Did Martin Fowler associate a smell with this? My favorite bad smell is "Inappropriate intimacy". Is that a fragrance from Calvin Klein?

DrLaszloJamfa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 12

> oh okay i understand wat you're saying, so do i have

> to put equal two..the reason i made not equal 2 is

> because i want my program to go back to the main

> menu..so what would the solution be here..(besides

> the use of methods) cause i only have 3-4 days left

> to summit this..

Anyway the problem is that you repeatedly have code like this

int x = 1;

while(x!=2){

if(x==0){

//code

}

if(x==1){

// code

}

}

But nowhere inside your while loop do you EVER change the value of X.

That means the loop will never exit. You need to change the value of X

inside the loop (in this case to 2) at some point if you want it to exit.

You do this in all your loops and that is the problem.

cotton.ma at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 13
okay guys..lol nuff said abt my messy code, anyway to fix my problem of going bac to main menus..cause now i dont kno
antonio_montanaa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 14
okay cotton m i get wat you're saying but how would i alter the boxes. (since quit is at 2, how would i change it)
antonio_montanaa at 2007-7-8 22:12:38 > top of Java-index,Java Essentials,New To Java...
# 15

> okay guys..lol nuff said abt my messy code,

To be honest though your messy code is problematic. We don't

complain for the sake of complaining (well doreme might but most of us

are quite kind), we complain because as you yourself are experiencing

it is a virtual nightmare to debug code like that.

3 or 4 days is more than enough time honestly to be able to refactor

(again refactoring means splitting methods into smaller methods) your

code. Which will probably give you a better grade, will certainly improve

your understanding of Java and will also certainly cause you to lose

less sleep in trying to fix what you have so far.

Either way though the advice given at least twice so far in how to fix your

code has been given. Do you understand what I said in my last post?

cotton.ma at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 16

> okay cotton m i get wat you're saying but how would i

> alter the boxes. (since quit is at 2, how would i

> change it)

Okay here is an example using a snippet of your code

int customerSelection = JOptionPane.showOptionDialog(null, customerMessage, "Security Central",0, JOptionPane.QUESTION_MESSAGE, null, customerValues, customerValues[0]);

while (customerSelection != 2) {

// your lengthy code block

blah blah blah

// NEW CODE AT END OF BLOCK

customerSelection = JOptionPane.showOptionDialog(null, customerMessage, "Security Central",0, JOptionPane.QUESTION_MESSAGE, null, customerValues, customerValues[0]);

}

cotton.ma at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 17
> Surely this is some sort of record!Do you not remember the one that had 150 levels of nesting ? Think someone was trying to test recusion or something...
Aknibbsa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 18
sry cotton m one more question, do i need to put customer selection at the end of the while loop or inbetween each if condition?
antonio_montanaa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 19

> sry cotton m one more question, do i need to put

> customer selection at the end of the while loop or

> inbetween each if condition?

End. You need it before the loop starts and at the end.

Please note something important though. You have this problem with

several (if not all) of your loops so you need to address this issue in all

cases with the correct code or you will still lock up.

cotton.ma at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 20

When you mean "addressing" u mean i must put those int values at the end of each while loop. Cause Im trying this and im still having some difficulty. I post that slot selection code..

while (playSelection != 3) {

//customerSelection = JOptionPane.showOptionDialog(null, customerMessage, "Security Central",

//0, JOptionPane.QUESTION_MESSAGE, null, customerValues, customerValues[0]);

if (playSelection == 0) {

String [] slotValues = {"YES", "NO"};

String slotMessage = "This game costs 100 Points. Do you want to play?";

int slotSelection = JOptionPane.showOptionDialog(null, slotMessage, "SLOTS",

0, JOptionPane.QUESTION_MESSAGE, null, slotValues, slotValues[0]);

while (slotSelection != 1) {

if (slotSelection ==0) {

balence[index] -= 100;

int num1 = 1 + (int) (Math.random()*7);

int num2 = 1 + (int) (Math.random()*7);

int num3 = 1 + (int) (Math.random()*7);

if (num1 == 7 && num1 == num2 && num1 == num3) {

balence[index] += 3000;

JOptionPane.showMessageDialog(null, "JACKPOT! YOU HAVE WON 3000 V-Points!");

}

if (num1 == num2 && num1 == num3 ) {

balence[index] += num1 * 300;

JOptionPane.showMessageDialog (null, "CONGRATULATIONS! YOU WIN! Your numbers are: " + num1 + " / " + num2 + " / " + num3 + "\n You win: " + num1*300 + " V-Points");;

} else {

JOptionPane.showMessageDialog (null,"Sorry. You didn't win. Better luck next time! \n Your numbers: "+ num1 + " / " + num2 + " / " + num3);

}

}

slotSelection = JOptionPane.showOptionDialog(null, slotMessage, "SLOTS",

0, JOptionPane.QUESTION_MESSAGE, null, slotValues, slotValues[0]);

}

}

is this the right place to put slotSelection?

Message was edited by:

antonio_montana

antonio_montanaa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 21
oops forget to [ c o d e ] it sry..the thing this is inside a loop called playSelection, will i have to also put that playselection int variable in the same place as well?Message was edited by: antonio_montana
antonio_montanaa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 22
say if i have one menu going into another. menu 1 and menu 2. wen im at menu 2, do i need that int variable for menu2 and menu1?Message was edited by: antonio_montana
antonio_montanaa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 23

> say if i have one menu going into another. menu 1 and

> menu 2. wen im at menu 2, do i need that int variable

> for menu2 and menu1?

If I understand your question, the answer is yes.

As several people suggested, it would help if you broke this code into several methods. Then your code would be much more readable:

private static final int CUSTOMER = 0;

private static final int ADMINISTRATION = 1;

private static final int MAIN_QUIT = 2;

public static void main(String [] args)

{

readNames();

// set up variables for the option pane

int selection = -1;

while (selection != MAIN_QUIT)

{

if (selection == CUSTOMER)

{

runCustomer();

}

else if (selection == ADMINISTRATION) // will always be true in this case

{

runAdministration();

}

selection = JOptionPane.showOptionDialog(null,

loginMessage, "Security Central",

0, JOptionPane.QUESTION_MESSAGE, null,

loginValues, loginValues[0]);

}

}

runCustomer would have something like:

private static void runCustomer()

{

int customerSelection = -1;

while (customerSelection != CUSTOMER_QUIT)

{

if (customerSelection == PLAY_NOW)

{

playGame();

}

else if (customerSelection == OPTIONS)

{

setCustomerOptions();

}

customerSelection = JOptionPane.showOptionDialog(null,

customerMessage, "Security Central",

0,

JOptionPane.QUESTION_MESSAGE, null,

customerValues, customerValues[0]);

}

}

playGame would be similar:

private static void playGame()

{

int playSelection = -1;

while (playSelection != BACK_TO_MAIN_MENU)

{

if (playSelection == SLOTS)

{

playSlots();

}

else if (playSelection == DOUBLE_OR_NOTHING)

{

playDoubleOrNothing();

}

else if (playSelection == LOTTERY)

{

playLottery();

}

int playSelection = JOptionPane.showOptionDialog(null,

playMessage, "USER OPTIONS",

0, JOptionPane.QUESTION_MESSAGE,

null, playValues, playValues[0]);

}

}

You could break some stuff down even farther, and you could use switch statements instead of "if..else if..else if...".

doremifasollatidoa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 24
"♥♥♥♥"
cotton.ma at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 25
> ""See? I do more than just complain.(Edit: Can I complain to Sun about how their quoting doesn't work?" Note that your lovely hearts didn't survive their quoting process.)
doremifasollatidoa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 26
> "♥♥♥♥"Works for me.
CaptainMorgan08a at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 27

> > "♥♥♥♥"

>

> Works for me.

I can't quote yours, either (though I see yours as hearts in both the original page and in the quote in the message box). If I view the source of the page, IE is rendering the original as four "& hearts;" entities, my quote as four "" [cents-sign and 3/4 fraction], and yours as "& #9829;" entities.

Edit: Okay, now I see that I can quote yours. The original "preview" that I did showed me that I couldn't quote yours (I got the same as my quote of cotton's). Can I complain to Sun that their preview is not accurate?

doremifasollatidoa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 28
> > ""> > Works for me.Edit: This is a second quote of CaptainMorgan's: Preview showed the cents/fraction, and now final shows that, too.
doremifasollatidoa at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 29

> > > ""

> >

> > Works for me.

>

> Edit: This is a second quote of CaptainMorgan's:

> Preview showed the cents/fraction, and now final

> shows that, too.

It must be your computer, then. Try copying/pasting the hearts into a text editor.

CaptainMorgan08a at 2007-7-21 16:35:44 > top of Java-index,Java Essentials,New To Java...
# 30
> > ""> > Works for me.IE mangling
cotton.ma at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 31
> ""IE from original
cotton.ma at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 32
> "♥♥♥♥"FF
cotton.ma at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 33
> > "♥♥♥♥"> > Works for me.FF
cotton.ma at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 34
lol idk wat u guys are doing? but thx i got it now..just had to fix the code, make it neater and a tad more legible..
antonio_montanaa at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 35
> ""From IE.
CaptainMorgan08a at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 36

"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥"

"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥ "

"♥♥♥♥♥♥♥♥♥♥♥♥ ♥♥ "

"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥"

"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥ ♥♥ "

"♥♥♥♥ ♥♥♥♥♥♥♥♥♥♥ "

"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥"

"♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥"

Try quoting that in IE.

CaptainMorgan08a at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 37
So in summary, in a highly surprising turn of events IE is mangling the characters. I am so shocked. You have no idea. Really. Shocked.
cotton.ma at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...
# 38

why don't you add another cofirm dialog box so the the player can enter his option to continue/ discontinue the game (or instead of the message dialog use a confirm dialog). so that the user can input his preferences and you will be able to change the value of slotselection variable

(i've given system.exit() here since you haven't added code for any other slotselection)

**********

while (customerSelection != 2) {

if (customerSelection ==0) {

String [] playValues = {"SLOTS","DOUBLE OR NOTHING", "LOTTERY", "BACK TO MAIN MENU"};

String playMessage = "What game would you like to play " + usernames[index] + "?";

int playSelection = JOptionPane.showOptionDialog(null, playMessage, "USER OPTIONS",

0, JOptionPane.QUESTION_MESSAGE, null, playValues, playValues[0]);

while (playSelection != 3) {

if(playSelection==1|playSelection==2)

{

System.exit(0);

}

if (playSelection ==0) {

String [] slotValues = {"YES", "NO"};

String slotMessage = "This game costs 100 Points. Do you want to play?";

int slotSelection = JOptionPane.showOptionDialog(null, slotMessage, "SLOTS",

0, JOptionPane.QUESTION_MESSAGE, null, slotValues, slotValues[0]);

int r=0;

while (slotSelection != 1) {

if (slotSelection ==0) {

balence[index] -= 100;

int num1 = 1 + (int) (Math.random()*3);

int num2 = 1 + (int) (Math.random()*3);

int num3 = 1 + (int) (Math.random()*3);

if (num1 == num2 && num1 == num3) {

balence[index] += num1 * 300;

JOptionPane.showMessageDialog (null, "CONGRATULATIONS! YOU WIN! Your numbers are:" + num1 + " / " + num2 + " / " + num3 + "\n You win" + num1*300);

r=JOptionPane.showConfirmDialog(null,"Do you wish to continue?");

if(r==1|r==2)

{

System.exit(0);

}

} else {

JOptionPane.showMessageDialog (null,"Sorry. You didn't win. Better luck next time! \n Your numbers: "+ num1 + " / " + num2 + " / " + num3);

r=JOptionPane.showConfirmDialog(null,"Do you wish to continue?");

if(r==1|r==2)

{

System.exit(0);

}

}

}

}

}

}

}

}

}

}

}

}

}

}

javabja at 2007-7-21 16:35:49 > top of Java-index,Java Essentials,New To Java...