Hey everyone,

New to Java and i was hoping if anyone could assist me.I needed to write a program that could display results of Champions League initial menu screen with the following set of options:
1. Display the current score for each possible response.
2. Vote
3. Quit the program.

import javax.swing.JOptionPane;
02	import java.util.Scanner;
03	 
04	public class ChampionLeague {
05	    public static void main(String args[]){
06	     
07	        int choice;
08	        int Quit = 0;
09	        String optionselected;
10	         
11	        String outputString = " ";
12	 
13	        outputString = JOptionPane.showInputDialog(" Who will win the Champions League in 2010/11? "
14	                                                                  + "\n 1. Real Madrid"
15	                                                                  + "\n 2. Barcelona"
16	                                                                  + "\n 3. Chelsea"
17	                                                                  + "\n 4. Manchester United");
18	 
19	        choice = lnterger.parselnt(outputString);
20	       
21	 
22	         for(int i = 0; i <4; i++){
23	          
24	                switch(choice ){
25	                 
26	             
27	                case 1: optionselected = "Real Madrid";
28	                break;
29	                  
30	                case 2: optionselected = "Barcelona";
31	                break;
32	                 
33	                case 3: optionselected = "Chelsea";
34	                break;
35	                 
36	                case 4: optionselected = "Manchester United";
37	                break;
38	                 
39	                default: optionselected = "Invalid number";
40	                 
41	             }//end of switch
42	        }// end of for
43	            
44	             
45	                     
46	        }
              }
47	         
Exception in thread "main" java.lang.UnsupportedOperationException: Not yet implemented
at lnterger.parselnt(lnterger.java:13)
at ChampionLeague.main(ChampionLeague.java:30)

I know i might have messed up somewhere but please understand that am new and trying to learn and any response will be highly appreciated..tips of good books for coding?

please advice

thanks
48	         
49

It is right where the error is. You are using lower case 'l' (L) which is not correct. What you need is capital letter 'I' as Integer.parseInt(...).

Yes i changed it' but now am getting this error
Exception in thread "main" java.lang.ExceptionInInitializerError
at ChampionLeague.main(ChampionLeague.java:30)
Caused by: java.lang.RuntimeException: Uncompilable source code - modifier private,static not allowed here
at lnterger.<clinit>(Interger.java:10)
... 1 more

If you look back, you would see that you attempt to assign a string to an uninitialized variable 'optionselected'. Go back to line 9 and assign something to your newly created String (i.e. String optionselected="";)...

By the way, please read what the compiling error is pointing. It is one of debugging message that you will come across a lot when you learn how to code in Java. The most important part of this error is the first couple lines when it tells you what kind of error and where it firstly encounters the problem.

I updated it and i don't get any error but it don't seem do display the result on the screen, rather it just prints the number of vote and shows the result on the keyboard display.

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

public class ChampionLeague {
    public static void main(String args[])throws IOException{
    
        int vote = 0;
        int Quit = 0;
        

        
        String outputString = " ";

        outputString = 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 = vote;


      if (x <= 1)

      System.out.println("\nYou Entered Real Madrid");

      else if (x <= 2)

      System.out.println("\nYou Entered Barcelona");

      else if (x <= 3)

      System.out.println("\nYou Entered Chelsea");

      else if (x <= 4)

      System.out.println("\nYou Entered Manchester United");


      else

     System.out.println("\nInvalid input\nEnter Valid Vote 1 - 4 ");



        JOptionPane.showMessageDialog(null, outputString );
        

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

Of course, it does nothing. You didn't obtain the value reading from the user. Also, even you did that, your program will accept an input from a user only once and display the result. Your current score is 0 for your display (vote). You just take the value of the initial value of 'vote' to your 'x'.

The showInputDialog() returns a string of the input. You need to parse the input to integer using Integer.parseInt(inputString).

// assuming that the user enter a valid input (number)
String input = JOptionPane.showInputDialog("blah blah 1.... 2.... 3...");
int x = Integer.parseInt(input);

By the way, the way you check for which choice the user has entered should be as followed...

if (x==1) {
  ...
}
else if (x==2) {
  ...
}
else if (x==3) {
  ...
}
else if (x==4) {
  ...
}
else {
  ...
}

One last thing, you display only what you receive from the user input. If you want the output to display whatever you want, you need to compose a string to be displayed to the JOptionPane. Currently, you also output on the console, not on your GUI display.

and if you don't like if-else-if-else ...., then just for Integer instance:

switch (integer) {
case 1:
//some stuff
case 2:
//......
case 3: 
// ...
case 4:
//...
default:
System.out.println("someWarning");
}

Hi everyone,
[Taywin] i corrected as your suggested but still not difference , it still displays the result in the keyboard.Could you please advice!

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

public class ChampionLeague {
    public static void main(String args[])throws IOException{
    
        int vote = 0;
        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)

      System.out.println("\nYou Entered Real Madrid");

      else if (x == 2)

      System.out.println("\nYou Entered Barcelona");

      else if (x == 3)

      System.out.println("\nYou Entered Chelsea");

      else if (x == 4)

      System.out.println("\nYou Entered Manchester United");


      else

     System.out.println("\nInvalid input\nEnter Valid Vote 1 - 4 ");

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

        JOptionPane.showMessageDialog(null, outputString );
        

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

The problem is your 'outputString' is not being assigned to anything. The reason I did not write anything inside the if-else statement because I wanted to see how much you understand the code. Anyway, it seems that you are still unclear about it. So the way to fix this is to append your outputString with those string you use in System.out.println().

if (x==1) {
  outputString = outputString + "You entered Real Madrid";
  // the above is the same as
  // outputString += "You entered Real Madrid";
else if (x==2) {
  ...
}
...

I have a feeling that you are still not completed this. I believe you are still missing a loop for accepting user input and counting the votes...

PS: You could use switch statement as mKorbel suggested. It works the same. Unless you don't feel comfortable or not sure about how it works.

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.