Hey Everyone,

Started Java for couple of months now and still struggling to put things together.Any assistant will be highly appreciated.

I wanted to display an initial menu screen with the following set of options:
1. Display the current score for each possible response.
2. Vote
3. Quit the program.

I wanted to display the user selects lets say the user selected option two, the program will display the question and the four possible responses eg Who will win the Champions League in 2010/11? "
+ "\n 1. Real Madrid"
+ "\n 2. Barcelona"
+ "\n 3. Chelsea"
+ "\n 4. Manchester United.

The user will then enter a response and the response will be recorded in the program.

this is what i have coded so far'

import javax.swing.JOptionPane;
import java.io.*;

public class ChampionLeague {
    static public int menu(String message, String menuOptions[]){
    
        int choice;
        int Quit = 0;
        String optionSelected = " ";
        String outputString = " ";
        
                
                //Show menu and get users choice
            choice = JOptionPane.showOptionDialog(null,  "Display the current score for each possible response \n"
                                                       + "2. Vote \n "
                                                       + "3. Quit the program ");
                                                        return choice;
                                                        
               int vote = 4;
               String game [][] = new String [choice][choice];     
               
               game [0][0] = "Real Madrid ";
               game [0][1] = "250 "; // display current vote
               
               game [1][0] = "Barcelona ";
               game [1][1] = " 320 ";
               
               game [2][0] = "Chelsea ";
               game [2][1] = "140 ";
               
               game [3][0] = "Manchester United ";
               game [3][1] = "300 ";
               
               for(int i = 0; i < choice; i++){
               String name = game[i][0];
               String marks = game[i][1];
        }
               JOptionPane.showMessageDialog(null, outputString );
        
        String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");

      
      

      int x = Integer.parseInt(input);
      

      if (x == 1)

      outputString = outputString + "You entered Real Madrid";

      else if (x == 2)

      outputString = outputString +("You Entered Barcelona");

      else if (x == 3)

      outputString = outputString + ("You Entered Chelsea");

      else if (x == 4)

      outputString = outputString + ("You Entered Manchester United");


      else

     outputString = outputString + ("Invalid input\nEnter Valid Vote 1 - 4 ");

       optionSelected = Integer.toString(x);
       System.out.println(x);

        JOptionPane.showMessageDialog(null, outputString );
        

                
        }//end of maim
    }// end of class

Have you even tried to compile this/test this before posting here?

Yes i did try to compile the array bit but it had errors .It said it doesn't have a main method,so i kept it in a separate file and added the below program in another file.Now am not sure how to connect/get the info from the array into the main program.

To make it give me the options'

please advice'

import javax.swing.JOptionPane;
import java.io.*;

public class ChampionLeague {
    public static void main(String args[])throws IOException{

        int choice;
        int Quit = 0;
        String optionSelected = " ";
        String outputString = " ";

        String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");

      
      

      int x = Integer.parseInt(input);
      

      if (x == 1)

      outputString = outputString + "You entered Real Madrid";

      else if (x == 2)

      outputString = outputString +("You Entered Barcelona");

      else if (x == 3)

      outputString = outputString + ("You Entered Chelsea");

      else if (x == 4)

      outputString = outputString + ("You Entered Manchester United");


      else

     outputString = outputString + ("Invalid input\n Enter Valid Vote 1 - 4");

       optionSelected = Integer.toString(x);
       System.out.println(x);

        JOptionPane.showMessageDialog(null,outputString );
        

                
        }//end of maim
    }// end of class

Hi, inni2662

I remove you comments from your first post and added mine. I do not understand.. your 1st post and your 2nd post are not same code... i start to change 1st post...
However there is something that i could not understand... there is point in your code where you start doing different tasks... like starting part when you ask user for action, and then suddenly you declare game array with length of USER DEFINED ACTION??? (User choose to quit and you declare array for voting??) and than you fill strings with names from array that depends on chosen user action. I corrected parts of your code just to have some starting point.
Now your code can be started, but it still contains huge errors. Also arrays could not be filled with this kind of code, as if user choose 1 than array game will be declared as game[1][2] and will fail in line with Chelsea with ArrayOutOfBoundsException because in code game is defined for only 2x2 fields.

Now from you i need info what this code should do... like this code seams little chaotic...

import javax.swing.*;
import java.io.*;

public class ChampionLeague {
    public  static void main(String[] args) //Java main class starts always with this procedure. This line is starting point of Java class.
{ 
        int choice;
        int Quit = 0; //It's better from start to adopt Java rules for variable names, or if you really need adopt your own rules but keep them same... if "choice" is with little "c" than "quit" should be with little "q". Java is case sensitive so all variable should be written same , it will be easier for you later to write code.
        String optionSelected = ""; // you do not need " " with space, "" is enough
        String outputString = "";
                
             choice=Integer.parseInt(JOptionPane.showInputDialog("1. Display the current score for each possible response \n " +
             													 "2. Vote \n " +
             													 "3. Quit the program ")); //Added Integer.ParseInt  showInputDialog return "String", choice is by definition Integer so INT=Integer.parseInt(STR);                   
               int vote = 4; //I really do not know what is this doing here??? And i do not know what is this for.. it is never used...
               String game [][] = new String [choice][2]; //choice = USER Action. It does not have anything with number of team... Seams like this is completly in wrong section of code. In normal case possible choices are [4][2] where [X][0] is Name and [X][1] is points... you do not need [X][4] matrix when only [X][2] is enough.
               
               game [0][0] = "Real Madrid ";
               game [0][1] = "250 "; 
	               game [1][0] = "Barcelona ";
	               game [1][1] = " 320 ";
               game [2][0] = "Chelsea ";
               game [2][1] = "140 ";
	               game [3][0] = "Manchester United ";
	               game [3][1] = "300 ";
// FROM here....               
               for(int i = 0; i < choice; i++)  
               {
            	   String name = game[i][0];
            	   String marks = game[i][1];
               }
//... till here it is a nonsense...
// you fill name and marks with all possible combinations. Not only that... game depends on type of action... CHOICE is 1. to display, 2 to VOTE 3 to quit
// it does not have any connection with teams...
//However neither name or marks is used down in code. If you do not need it, do not code it...
               
               JOptionPane.showMessageDialog(null, outputString ); //At this point outputString is "" as you declare it. It is not use till this moment...
        
        String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");
//      int input = Integer.parseInt(JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
//        + "\n 1. Real Madrid"
//        + "\n 2. Barcelona"
//        + "\n 3. Chelsea"
//        + "\n 4. Manchester United"));
//  This is my view of code and solely my opinion... you declare String input, later you declare x as int and convert string input to int x...
        // if you declare input as integer and use Integer.parseInt(String) as above you will shortened your code...
      int x = Integer.parseInt(input);

//FROM here...
      if (x == 1) //Also you can use switch... 
    	  outputString = outputString + "You entered Real Madrid";
      else if (x == 2)
    	  outputString = outputString +("You Entered Barcelona");
      else if (x == 3)
    	  outputString = outputString + ("You Entered Chelsea");
      else if (x == 4)
    	  outputString = outputString + ("You Entered Manchester United");
      else
    	  outputString = outputString + ("Invalid input\nEnter Valid Vote 1 - 4 "); //This is problem... on Enter valid vote you should return user to choice... but for that later... we are far from that...
//TILL here....
      //you can replace it with ...
//      if (x>=1 && x<=4)
//    	  outputString = outputString + "You entered " + game[x][0]; //This code uses array that you specify above... if you code it.. use it...
//      else	
//    	  outputString = outputString + ("Invalid input\nEnter Valid Vote 1 - 4 "); 
// This shortens you code.. what if you have 20 teams... will you write 20 else if's?... You decalre array, use it      

      optionSelected = Integer.toString(x); // This is same as  optionSelected=input  
      // REASON: because x = Integer.parseInt(input) and optionSelected=Integer.toString(x) it is same
      
       System.out.println(x); 

        JOptionPane.showMessageDialog(null, outputString ); 
        

                
        }
    }

Hi monarchmk,
Sorry for the confusion and thanks for your comments.I wanted to display an initial menu screen with the following set of options:
1. Display the current score for each possible response.
2. Vote
3. Quit the program.

Thats why i added //Show menu and get users choice
choice = JOptionPane.showOptionDialog(null, "Display the current score for each possible response \n"
+ "2. Vote \n "
+ "3. Quit the program ");
return choice;

So that to get the options from the user of displaying the current scores, thats why i thought of adding arrays.Then i wanted the user to vote there team and the result of the vote to be added in the program above.If i got all 2 right then it will be simple to do the 3.quit program.

I'll try to make you a plan how code in this class should be positioned...

1. Variable definition. All variables should be here (more or less, not strict rule)
2. Fill arrays with start values. (array should be prepared for later use. For start maybe it will be easier if you use 1 array for names and 1 for scores.. like String game[] for names and int score[] for votes and fill it parallel.)
3. Define User Action (Ask user what to do and do some action according to user choice)
. 3a. Display score
. 3b. Vote
. 3c. Quit

IF 3a is selected do...
. print all arrays with current values... FOR cycle...
IF 3b is selected do...
. Display all names to user ( array[X][0]) ... FOR cycle
. Ask user for vote ...showinputdialog
. Increment vote for team
. Return to menu "user actions" (3)
IF 3c quit code

Here is your code in clean version. I won't write code for you, but I'll be here to help you all the way with your code
First try to reposition your code by above plan... or you can redefine plan if you like, but reposition you code by final plan.
It does not matter if its not working from start... we'll have to add some more function to code to work...

import javax.swing.*;
import java.io.*;

public class ChampionLeague {
    public  static void main(String[] args)
{ 
        int choice;
        int quit = 0; 
        String optionSelected = ""; 
        String outputString = "";
                
        choice=Integer.parseInt(JOptionPane.showInputDialog("1. Display the current score for each possible response \n " +
             													 "2. Vote \n " +
             													 "3. Quit the program "));                   
               String game [][] = new String [4][2];                
               game [0][0] = "Real Madrid ";
               game [0][1] = "250 "; 
	               game [1][0] = "Barcelona ";
	               game [1][1] = " 320 ";
               game [2][0] = "Chelsea ";
               game [2][1] = "140 ";
	               game [3][0] = "Manchester United ";
	               game [3][1] = "300 ";
//               JOptionPane.showMessageDialog(null, outputString ); 
        String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");
      int x = Integer.parseInt(input);

      if (x>=1 && x<=4)
    	  outputString = outputString + "You entered " + game[x][0];
      else	
    	  outputString = outputString + ("Invalid input\nEnter Valid Vote 1 - 4 "); 


      optionSelected = input; 
       System.out.println(x); 
        JOptionPane.showMessageDialog(null, outputString ); 
        }
    }

Hi monarchmk,
Am trying to let the user select an option and i thought of using if else if the select option 1 it adds 1 to the vote.

import javax.swing.JOptionPane;
import java.io.*;

public class ChampionLeague {
    private static String input;

    public static void main(String args[]){

        int choice;
        int quit = 0;
        String optionSelected = "";
        String outputString = " ";
        
        choice=Integer.parseInt(JOptionPane.showInputDialog("1. Display the current score for each possible response \n"
                                                               +"2. Vote \n " 
             	                                               +"3. Quit the program "));
             													                    
               String game [][] = new String [4][2];                
               game [0][0] = "Real Madrid ";
               game [0][1] = "250 ";
               
	       game [1][0] = "Barcelona ";
	       game [1][1] = " 320 ";
               
               game [2][0] = "Chelsea ";
               game [2][1] = "140 ";
               
	       game [3][0] = "Manchester United ";
	       game [3][1] = "300 ";


               
              String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");


       int x = Integer.parseInt(input);
       int result = x + 1;

       if (x>=1 && x<=4)
       outputString = outputString + "You entered " + game[x][0];

       else if(x == 1)
           outputString = outputString + "Score :"  + result;
       else
           outputString = outputString + ("Invalid input\nEnter Valid Vote 1 - 4 ");

       optionSelected = input;
       System.out.println(x);

        JOptionPane.showMessageDialog(null,outputString );

      }//end of main

  }// end of class

Doesn't seem to do as i asked.

hmmm...

Did not read whole post...

move this code

choice=Integer.parseInt(JOptionPane.showInputDialog("1. Display the current score for each possible response \n"
                                                               +"2. Vote \n " 
             	                                               +"3. Quit the program "));

below game[][] array definition... Array should e defined before anything..

how i'll give you an example what i think to do

i'll use WHILE condition... and SWITCH... just to make sample how this command can be used and implement in your code
I commented whole code.. copy code and try to execute it , and after that try to implement WHILE and SWITCH to your code (SWITCH will replace those IF... chains)

import java.io.*;
import javax.swing.*;  
public class monarch {

	public static void main(String[] args) throws Exception
	{
		while (true) // real syntax is WHILE (condition)... i'll put true for infinite loop// I'll control it via break; command later in code
			// with break; you tell Java to exit from loops (applies to FOR also)
		{
			int question=Integer.parseInt(JOptionPane.showInputDialog("Please enter your choice: \n " +
					"1. something...\n " +
					"2. somthing else.... \n " +
					"3. something else too... \n " +
					"4. QUIT"));
// Here i declare question as integer so later i would not have to convert string value to integers for comapartion.
			//First we will check if user need program termination...
			if (question==4) 
				{
				JOptionPane.showMessageDialog(null,"You choose to quit... bye..."); //Inform user that we quit and...
				break; //end of program... with break you terminate while true condition and exit the loop
				}
			
			//If not we will check what user choose to do... options 1,2 or 3
			switch (question) // switch need numberic parameter for evaluation. In example here question is INT so depends on question value switch will execute 
			// commands in lines case XX where XX is question value.
			{
			case 1: // If question has value of 1 do this.
				{
					JOptionPane.showMessageDialog(null,"You choose 1... here i'll do whatever is needed for option 1"); //Here you can put whatever code you need to be done if user choose 1
					break; //Do not be confused. this break breaks SWITCH command and return us to question again
				}
			case 2: // If question has value of 2 do this...
				{
					JOptionPane.showMessageDialog(null,"You choose 2... here i'll do whatever is needed for option 2");//Here you can put whatever code you need to be done if user choose 2
					break; //This break also terminates SWITCH and return us to question
				}
			case 3: // If question has value of 3 do this...
				{
					JOptionPane.showMessageDialog(null,"You choose 3... here i'll do whatever is needed for option 3");//Here you can put whatever code you need to be done if user choose 3
					break;
				}
			default: //this is like else... if user input a value out of range that we defined (lower than 1 or bigger than 4 in this case
				{
					JOptionPane.showMessageDialog(null, "You choose non vaid imput... try again.."); 
					break;
				}

			}
			
		}
	}
}

Hi
I tried to follow your instructions, but i wanted the switch statement in case 2 to display the option of "You voted Barcelona" and in case 1 i couldn't figure out how to display each score separately thats why i included all results.

i came up with this but its working half way :-(

import javax.swing.JOptionPane;
import java.io.*;

public class ChampionLeague {
    private static String input;

    public static void main(String args[])throws Exception{

        int choice;
        String optionSelected = "";
        String outputString = " ";
        
       	                    
           while (true) {

           choice=Integer.parseInt(JOptionPane.showInputDialog ("1. Display the current scores \n"
                                                               +"2. Vote \n "
             	                                               +"3. Quit the program "));

           if (choice == 3) {
				JOptionPane.showMessageDialog(null,"You choose to quit... Goodbye...");

                                break;
                         }//end of if
           switch(choice){
               
               case 1:
                   JOptionPane.showMessageDialog(null," Current Score for Real Madrid is: 6\n "
                                                    + "Current Score for Barcelona is: 5\n "
                                                    + "Current Score for Chelsea is : 4\n "
                                                    + "Current Score for Manchester United is: 3\n ");
                   break;
                   
               
               case 2:
                   String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");
                   break;
                   
               case 3:
                   JOptionPane.showMessageDialog(null,"You choose to Quit! Have a lovely day ! ");
                   break;
                   
               default:
                   JOptionPane.showMessageDialog(null, "Invalid input... try again..");
					break;
           
           
           }//end of switch
            int x = Integer.parseInt(input);

                  if (x == 1) 
    	                 outputString = outputString + "You entered Real Madrid";
                  else if (x == 2)
    	                 outputString = outputString +("You Entered Barcelona");
                  else if (x == 3)
    	                  outputString = outputString + ("You Entered Chelsea");
                  else if (x == 4)
    	                  outputString = outputString + ("You Entered Manchester United");
                  else
    	                   outputString = outputString + ("Invalid input\nEnter Valid Vote 1 - 4 ");

        }// end of while
           
      }//end of main

  }// end of class

Ok... you are almost done :)
You are missing this code.. I rearanged a little and add new array...

String game [] = new String [5];                
               int score[] = new int [5];
               game [1] = "Real Madrid ";
               score [1] = 250;
               
	       game [2] = "Barcelona ";
	       score [2] = 320;
               
               game [3] = "Chelsea ";
               score [3] = 140;
               
	       game [4]= "Manchester United ";
	       score [4]= 300;

This should be in your code... before while...
I aligned array numbers with vote question in line 36-40 (You asked 1 for Real 2 for Barcelona so i changed array game to be same as your question game[1]=Real, [2]=Barcelona...). Learn that code is yours... It is, maybe, "rule" that array starts from 0 but if you need array to start from other number... use it from that number, do not use arrays before it,that is not a mistake.

And also delete lines 53-65.. IF is not needed here. When you exit switch you are returned to while... code out of switch here is meaningless because in SWITCH we determine user actions and act upon them. Out of switch (in this code) we do not need anything, there is question what user like to do and that is all we need out of switch.

You using switch..and while... when user enter that he like to VOTE you code goes to switch and enters case 2:... now in case 2 you ask for what team user likes to vote...
Here.. when user enters let say 2 for Barcelona.. you need to display

int voted=Integer.parseInt(input); //We need integer for arrays
                   JOptionPane.showMessageDialog(null, "You vote for " + game[voted] + ". \n Now " + game[voted] + " has " + ++score[voted]);

Where INPUT is answer from question but is converted to integer as for array we need integer value. Here in two line (without multiple IF's) you have solution for all teams. Do you know why i used ++score[voted]?

I used array with variable so what ever team user enters i'll have an answer. (not quite true, if user enter 5 there will be outofbound exception... for that we will have to enter some error handling and checking but it is not necessary now, lets first make code to work.).

Also lines 28-31 displays static values... you should replace 6 with " + score[1] + "
or you can replace your message with FOR cycle where you will fill some string with array values... but for that later ...

Ok, try to make changes. After this you code will work.

P.S. Line 43-46 (case 3: in switch) will never be used. It is not mistake but code will work same without those lines. Do you know why line 43-46 are not needed?

Hi
I think You used ++score cause it will increment score by 1 and then use the new value in the expression in which score resides.case 3(switch) will not be used as they is the default which if the use enter an invalid input it will display the invalid statement.

I code this but case 2 is giving a NumberFormatException error

import javax.swing.JOptionPane;
import java.io.*;

public class ChampionLeague {
    private static String input;
  
    public static void main(String args[])throws Exception{

     int choice;
        

               String game [] = new String [5];
               int    score[] = new int [5];

               game  [1] = "Real Madrid ";
               score [1] = 250;

	       game  [2] = "Barcelona ";
	       score [2] = 320;

               game  [3] = "Chelsea ";
               score [3] = 140;

	       game  [4]= "Manchester United ";
	       score [4]= 300;
        
       	                    
           while (true) {

           choice=Integer.parseInt(JOptionPane.showInputDialog ("1. Display the current scores \n"
                                                               +"2. Vote \n"
             	                                               +"3. Quit the program "));

           if (choice == 3) {
				JOptionPane.showMessageDialog(null,"You choose to quit... Goodbye...");

                                break;
                         }//end of if
           switch(choice){
               
               case 1:
                   JOptionPane.showMessageDialog(null,"Current Score for Real Madrid is: " + score[1]+"\n"
                                                    + "Current Score for Barcelona is: "  + score[2]+ "\n"
                                                    + "Current Score for Chelsea is :"+ score[3]+ "\n"
                                                    + "Current Score for Manchester United is: "+ score[4] + "\n");
                   break;
                   
               
               case 2:
                   int voted=Integer.parseInt(input);
                        JOptionPane.showMessageDialog(null, "You vote for "
                                                      + game[voted] + ". \n Now "
                                                      + game[voted] + " has "
                                                      + ++score[voted]);
                                                                 
                   break;
                   
               case 3:
                   JOptionPane.showMessageDialog(null,"You choose to Quit! Have a lovely day ! ");
                   break;
                   
               default:
                   JOptionPane.showMessageDialog(null, "Invalid input... try again..");
					break;
           
           
           }//end of switch
            

        }// end of while
           
      }//end of main

  }// end of class

Hi,

you did not read while post... I wrote

...You using switch..and while... when user enter that he like to VOTE you code goes to switch and enters case 2:... now in case 2 you ask for what team user likes to vote...
Here.. when user enters let say 2 for Barcelona.. you need to display
...

but i never tell you to delete this part of code :)
When you removed menu for voting, you remove input variable declaration and that's why you have number exception...

put this code back

String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");

between line 49 and 50
and you are done program works...
For ++score you are ok, it is increment value of score than display new value, score++ is display score than increment value
But for case 3 no...
Now please remove case 3 (line 58-60) it will never execute because you intercept user answer for quit (3) in line 34 and quit program before it enters switch loop.

Like an extra for this code you can replace message in case 1: (lines 42-45) with this code

String messg="";
                   for (int a=1;a<game.length;a++)
                	   messg +=" Current Score for " + game[a] + " is: " + score[a] + " \n";
            	   JOptionPane.showMessageDialog(null,messg);

If you later add new team(s) you would not have to change this code as it will print all teams that are defined with game array.

commented: Excellent step by step guidance +1

Hi monarchmk
Program works perfectly ' thank you' now it all makes sense, i never knew i could add array code to switch cases..

case 2:
                   String input = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
                                                                  + "\n 1. Real Madrid"
                                                                  + "\n 2. Barcelona"
                                                                  + "\n 3. Chelsea"
                                                                  + "\n 4. Manchester United");
                   int voted=Integer.parseInt(input);
                        JOptionPane.showMessageDialog(null, "You vote for "
                                                      + game[voted] + ". \n Now "
                                                      + game[voted] + " has "
                                                      + ++score[voted]);

I hope this program will help a lot of new learners of java!

Thanks ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.