Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 27, 2006, 10:04 AM   #1
Jasonbot
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
Java JOptionPane+ifs

Basically I want the JOptionPane "//main window" to take me back to the start but not to open a new instance of the program. see the red
Code:
	
	//start
	String name=JOptionPane.showInputDialog(null,"Enter your name");
	JOptionPane.showMessageDialog(null,"Welcome " +name);
				
	//move(main window)
	String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
	int go = Integer.parseInt(move);
				
	//SHOP
	if(go==1)
	{
					
		String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
		int sell=Integer.parseInt(sell1);
		//sell right
		if(sell==1)
		{
		JOptionPane.showMessageDialog(null,"1");	
		}
		//sell left
		else if (sell==2)
		{
		JOptionPane.showMessageDialog(null,"2");
		}
					
		//main window
		else if (sell==3)
		{
		//go(main window)
		 move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
		 go = Integer.parseInt(move);
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 27, 2006, 11:56 AM   #2
Kunimodi
macrumors member
 
Join Date: Sep 2006
Location: Ashland, OR, USA
Hi, Jasonbot. There are several ways that you could structure your program to return to the main menu. Probably the way that would involve the least amount of change to your program would be to add the following to put a loop around the main menu handler with the [optional] addition of an exit option:

Code:
do
{
    String move=JOptionPane.showInputDialog(null, "1=Shop"+"\n2=Playground" +"\n3=Buy stock\n4=Exit");
    int go = Integer.parseInt(move);
    if (go == 1)
    {
        // shop handler
    } else if (go == 2) {
       // and so on
   }
} while (go != 4);
Kunimodi is offline   0 Reply With Quote
Old Sep 27, 2006, 12:34 PM   #3
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
WOW! It works, can't believe it!!!! Can you maybe explain what this loop is doing. My understanding is that it loops until go is not equal to 4??? why 4?
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 27, 2006, 12:44 PM   #4
Kunimodi
macrumors member
 
Join Date: Sep 2006
Location: Ashland, OR, USA
Quote:
Originally Posted by Jasonbot
WOW! It works, can't believe it!!!! Can you maybe explain what this loop is doing. My understanding is that it loops until go is not equal to 4??? why 4?
Actually, I had a bug 'int go' should be declared before the loop or it won't be visible to the while clause (e.g. int go; do { } while (go != 4); }

I added 4 to the input dialog as the number to exit the program. Yes, it runs until go is 4.
Kunimodi is offline   0 Reply With Quote
Old Sep 27, 2006, 12:46 PM   #5
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
Ok, cool! but now I can't leave shop to go to playground! I put the do inside the if, is that a problem? I tried with do instide if but then you can't get to one or the other!

Code:
//move(main window)
				String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
				int go = Integer.parseInt(move);
				
				if(go==2)
				{
				do{
				//Playground (find stuff)
				
				
					String play1=JOptionPane.showInputDialog(null,"In the playground" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int play=Integer.parseInt(play1);
					
					//play left
					if(play==1)
					{
						JOptionPane.showMessageDialog(null,"1");
					}
					
					//play right
					else if(play==2)
					{
						JOptionPane.showMessageDialog(null,"2");
					}
					
					//main menu
					else if(play==3)
					{
						//go(main window)
						move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
					}
					
					
				}while(go != 4);
				}
				
				if(go==1)
				{
				do{
				//SHOP
					String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int sell=Integer.parseInt(sell1);
					
					//sell right
					if(sell==1)
					{
					JOptionPane.showMessageDialog(null,"1");
					}
					
					//sell left
					else if (sell==2)
					{
					JOptionPane.showMessageDialog(null,"2");
					}
					
					//main window
					else if (sell==3)
					{
						//go(main window)
						move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
						
					}
					
					
				}while(go != 4);
				}
__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 27, 2006, 01:37 PM   #6
mufflon
macrumors 6502
 
Join Date: Sep 2006
the error on that code is clearly a so called "logical" issue, where the code is like this:

Code:
   if num = 2
      do until num is 4
   (num is here 1 or 4)
   if num is 1 (first part of code haven't occured)
       do until code is 4
   (num here should be 4)
as you can see this is not optimal.

what you can do is the following:
Code:
class test {
   private int currentCommand;
   // simply all numbers and other variables here
   // you reach em by writing this.*name of variable*
   public void shop() {
      if (this.currentCommand == 1) {
          // do first part of code. end it with "break"
      } else if(currentCommand == 2) {
          // do second part of code here
      } else {
          // neither occurs for unknown reason.
          // reset to first command
          this.currentCommand = 1;
      }
      if(this.currentCommand != 3) {
          // if command is 3 this is not executed
          // we call back to this function if command
          // isn't 3.
          // 3 == application stops
          this.shop()
       }
   }
}
a better way to code this imo.

Code:
move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
you should use a while loop such as this:

[CODE]
if(num == 4) {
do {
//stuff
} while(num == 4); // when someone don't want to be here he'll leave
// if he wants to stay he simply presses same direction again (4)
// which seems to have been your intention with the dialogue
}
mufflon is offline   0 Reply With Quote
Old Sep 27, 2006, 01:47 PM   #7
MarkCollette
macrumors 65816
 
MarkCollette's Avatar
 
Join Date: Mar 2003
Location: Calgary, Canada
You might want to search for "state machine" or "finite state machine", since I think that's what you're trying to make.
MarkCollette is offline   0 Reply With Quote
Old Sep 27, 2006, 01:58 PM   #8
mufflon
macrumors 6502
 
Join Date: Sep 2006
oh Kunimodi had already helped you, jason, you just seem to have placed the while loop in the wrong place.

it's like this:

Code:
do
    what I want
while I still want to do something
this is what the do/while loop does, written in plain english.
mufflon is offline   0 Reply With Quote
Old Sep 27, 2006, 01:58 PM   #9
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
I'll try it tomorrow, but before then I just need some more basic terminology:


public void shop() <--???

this.currentCommand <---was this just a variable name? or is this. important?

OK, it's still not tomorrow but I tried the *plain english* alternative, my code now only goes to the first option AKA only if you input "1" in the Input dialog. If you try "2" or "3" it dies

Code:
				//move(main window)
				String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
				int go = Integer.parseInt(move);
				
				do{
					if(go==1)
					{
				//SHOP
					String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int sell=Integer.parseInt(sell1);
					
					//sell right
					if(sell==1)
					{
					JOptionPane.showMessageDialog(null,"1");
					}
					
					//sell left
					else if (sell==2)
					{
					JOptionPane.showMessageDialog(null,"2");
					}
					
					//main window
					else if (sell==3)
					{
						//go(main window)
						move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
						go = Integer.parseInt(move);
						
					}
					
					
					}}while(go != 4);
				do{
				//Playground (find stuff)
					
					if(go==2)
					{
						
						String play1=JOptionPane.showInputDialog(null,"In the playground" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
						int play=Integer.parseInt(play1);
						
					//play left
						if(play==1)
						{
							JOptionPane.showMessageDialog(null,"1");
						}
						
					//play right
						else if(play==2)
						{
							JOptionPane.showMessageDialog(null,"2");
						}
						
					//main menu
						else if(play==3)
						{
						//go(main window)
							move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
							go = Integer.parseInt(move);
						}
						
						
					}}while(go != 4);

				 do{
				//BuyStock(buy stuff to sell)
					 
					 if(go==3)
					 {
						 
						 String play1=JOptionPane.showInputDialog(null,"At the shop" +"\n1=Buy Food" +"\n2=Buy drink" +"\n3=Main Menu");
						 int play=Integer.parseInt(play1);
						 
					//buy drink
						 if(play==1)
						 {
							 JOptionPane.showMessageDialog(null,"1");
						 }
						 
					//buy food
						 else if(play==2)
						 {
							 JOptionPane.showMessageDialog(null,"2");
						 }
						 
					//main menu
						 else if(play==3)
						 {
						//go(main window)
							 move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
							 go = Integer.parseInt(move);
						 }
						 
						 
					 }}while(go != 4);
__________________
[insert apple related products here]

Last edited by Jasonbot; Sep 27, 2006 at 02:07 PM.
Jasonbot is offline   0 Reply With Quote
Old Sep 27, 2006, 02:07 PM   #10
mufflon
macrumors 6502
 
Join Date: Sep 2006
the currentCommand is declared outside of the method (the uppermost part of the code, "private int currentCommand") and is a so called global variable, which can be reached by any method within the overlaying class (in this case the class is "test")

public void shop() is a so called method in the class test. The name of the method is shop(), void is what it returns (void means nothing) and public means any code or application, even ones outside of the class, can reach it.

Code:
test something = new test();
// creates a new instance of "test"
something.shop() 
// code within "something" is called.
mufflon is offline   0 Reply With Quote
Old Sep 27, 2006, 02:10 PM   #11
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
This is probably what you are looking for:

Code:
public class MyApp {

    private static String NAME_MESSAGE = "Enter your name:";
    private static String MOVE_MESSAGE = "1=Shop\n2=Playground\n3=Buy stock";

    private String getUsername() {
        return JOptionPane.showInputDialog(null, NAME_MESSAGE);
    }

    private int getMove() {
        String move = JOptionPane.showInputDialog(null, MOVE_MESSAGE);
	return Integer.parseInt(move);
    }

    private void doShop() {
        System.out.println("Shopping");
        // do shopping stuff here.
    }

    private void doPlay() {
        System.out.println("Playing");
        // do playing stuff here.
    }

    private void doBuyStock() {
        System.out.println("Buying Stock");
        // do stock stuff here.
    }

    private void run() {
        //start
	String name=getUsername();

	JOptionPane.showMessageDialog(null,"Welcome " + name);

        boolean continueMoving = true;
        while(continueMoving) {
            int move = getMove();

            switch(move) {
                case 1:
                    this.doShop();
                    break;
                case 2:
                    this.doPlay();
                    break;
                case 3:
                    this.doBuyStock();
                    break;
                case 4:
                    continueMoving = false;
                    break;
                default:
                    // This is what happens if move != 1, 2, 3, or 4.
                    break;
            }
        }

    }

    public static void main(String[] argv) {
        MyApp instance = new MyApp();
        instance.run();
    }

}
mrichmon is offline   0 Reply With Quote
Old Sep 27, 2006, 02:23 PM   #12
Jasonbot
Thread Starter
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
I do want code like that but instead of printing lines I want to get values and use them (variables) here's a diagram of what needs to happen:

Main is the root selector:
Code:
String move=JOptionPane.showInputDialog(null, "1=Shop" +"\n2=Playground" +"\n3=Buy stock");
				int go = Integer.parseInt(move);
options is the options:
Code:
String sell1=JOptionPane.showInputDialog(null,"Sell Stuff" +"\n1=Move left" +"\n2=Move Right" +"\n3=Main Menu");
					int sell=Integer.parseInt(sell1);
and more is the variables that will come out of the option selected, I'll use a random number(1,2 or 3) to get an "item" for the "inventory" but the main thing is that it must be able to go back to the beginning when you want to.


__________________
[insert apple related products here]
Jasonbot is offline   0 Reply With Quote
Old Sep 27, 2006, 02:26 PM   #13
mufflon
macrumors 6502
 
Join Date: Sep 2006
well we've all made code example of exactly what you describe, read through em, ask questions if you like - we're here and both ready and willing to help
mufflon is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
programming ifs help. farmerdoug Mac Programming 17 Nov 11, 2012 04:31 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 08:18 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC