Welcome to Dream.In.Code
Become a Java Expert!

Join 149,615 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,858 people online right now. Registration is fast and FREE... Join Now!




Help with switch

 
Reply to this topicStart new topic

Help with switch

tygerberg
19 Aug, 2007 - 12:59 PM
Post #1

D.I.C Head
Group Icon

Joined: 14 Dec, 2006
Posts: 114


Dream Kudos: 25
My Contributions
Hi
I'm pretty bad at using switch, this program is an example, could someone please help me.

CODE
import javax.swing.JOptionPane;
public static void main(String[] args) {
        
        int x1,x2,ans;
        char choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit");
        
        switch(choice)
        {
        case ('A'):
        case ('a'):
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
                   ans = x1 + x2;
                   JOptionPane.showMessageDialog(null,"The answer is: "+ans);
                   break;
        case ('S'):
        case ('s'):
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
                   ans = x1 - x2;
                   JOptionPane.showMessageDialog(null,"The answer is: "+ans);
                   break;
        case ('D'):
        case ('d'):
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
                   ans = x1 / x2;
                   JOptionPane.showMessageDialog(null,"The answer is: "+ans);
                   break;  
        case ('E'):
        case ('e'):
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
                   ans = x1 * x2;
                   JOptionPane.showMessageDialog(null,"The answer is: "+ans);
                   break;
        case ('X'):
        case ('x'):
                   System.exit(0);
        default:
                   JOptionPane.showMessageDialog(null,"OH");
       }
       System.exit(0);      
    }
    
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Help With Switch
19 Aug, 2007 - 01:03 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Absolutely...can you specify the problem? Are you getting error messages? What are they? Or are you seeing undesired behaviour?
User is offlineProfile CardPM
+Quote Post

tygerberg
RE: Help With Switch
19 Aug, 2007 - 01:06 PM
Post #3

D.I.C Head
Group Icon

Joined: 14 Dec, 2006
Posts: 114


Dream Kudos: 25
My Contributions
Well using netbeans I get the following:

CODE
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:14: incompatible types
found   : java.lang.String
required: int
        int choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:20: incompatible types
found   : java.lang.String
required: int
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:21: incompatible types
found   : java.lang.String
required: int
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:27: incompatible types
found   : java.lang.String
required: int
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:28: incompatible types
found   : java.lang.String
required: int
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:34: incompatible types
found   : java.lang.String
required: int
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:35: incompatible types
found   : java.lang.String
required: int
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:41: incompatible types
found   : java.lang.String
required: int
                   x1 = JOptionPane.showInputDialog("Enter first Number: ");
C:\Documents and Settings\Gareth\My Documents\My Games\Switch\src\switchpkg\Main.java:42: incompatible types
found   : java.lang.String
required: int
                   x2 = JOptionPane.showInputDialog("Enter second Number: ");
9 errors
BUILD FAILED (total time: 0 seconds)

User is offlineProfile CardPM
+Quote Post

alpha02
RE: Help With Switch
19 Aug, 2007 - 01:27 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
The following code is wrong:

CODE
int x1 = JOptionPane.showInputDialog(...);


The showInputDialog method returns a String and not a int. The showConfirmDialog returns an int. For more info, see:

http://www.dreamincode.net/forums/showtopic22739.htm

Also, I doubt the line:

CODE
char choice = JOptionPane.showInputDialog(...);


will always give the desired result. You'd better use:

CODE
char choice = JOptionPane.showInputDialog(...).toLowerCase().toCharArray()[0];


In the switch, put only:

case 'a':
case 's':
case 'm':
case 'd':

instead of having also uppercase statements, because we converted the string to lowercase in the line I told you to use smile.gif Any questions then just reply!

This post has been edited by alpha02: 19 Aug, 2007 - 01:31 PM
User is offlineProfile CardPM
+Quote Post

tygerberg
RE: Help With Switch
20 Aug, 2007 - 08:04 AM
Post #5

D.I.C Head
Group Icon

Joined: 14 Dec, 2006
Posts: 114


Dream Kudos: 25
My Contributions
Thanks a lot it really helped me learn the switch. Here's my final app.

CODE
import javax.swing.JOptionPane;
public class Switch
{
    public static void main (String[] args)
    {
       String x1, x2;
    int ans, x11, x22;
        
    char choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit").toLowerCase().toCharArray()[0];
    
    do {
    switch(choice)
    {
    case ('a'):
           x1 = JOptionPane.showInputDialog("Enter first Number: ");
           x2 = JOptionPane.showInputDialog("Enter second Number: ");
           x11 = Integer.parseInt(x1);
           x22 = Integer.parseInt(x2);
           ans = x11 + x22;
           JOptionPane.showMessageDialog(null,"The answer is: "+ans);
           choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit").toLowerCase().toCharArray()[0];
           break;
    case ('s'):
           x1 = JOptionPane.showInputDialog("Enter first Number: ");
           x2 = JOptionPane.showInputDialog("Enter second Number: ");
           x11 = Integer.parseInt(x1);
           x22 = Integer.parseInt(x2);
           ans = x11 - x22;
           JOptionPane.showMessageDialog(null,"The answer is: "+ans);
           choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit").toLowerCase().toCharArray()[0];
           break;
    case ('d'):
           x1 = JOptionPane.showInputDialog("Enter first Number: ");
           x2 = JOptionPane.showInputDialog("Enter second Number: ");
           x11 = Integer.parseInt(x1);
           x22 = Integer.parseInt(x2);
           ans = x11 / x22;
           JOptionPane.showMessageDialog(null,"The answer is: "+ans);
           choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit").toLowerCase().toCharArray()[0];
           break;  
    case ('e'):
           x1 = JOptionPane.showInputDialog("Enter first Number: ");
           x2 = JOptionPane.showInputDialog("Enter second Number: ");
           x11 = Integer.parseInt(x1);
           x22 = Integer.parseInt(x2);
           ans = x11 * x22;
           JOptionPane.showMessageDialog(null,"The answer is: "+ans);
           choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit").toLowerCase().toCharArray()[0];
           break;
    case ('x'):
           System.exit(0);
    default:
           JOptionPane.showMessageDialog(null,"That option was not available. Try again.");
           choice = JOptionPane.showInputDialog("Menu\n---\nA. Add\nS. Subtract\nD. Divide\nE. Multiply\nX. Exit").toLowerCase().toCharArray()[0];
          
                      
          
       }
           } while (choice != 'n');
    }
        
} // main method



User is offlineProfile CardPM
+Quote Post

alpha02
RE: Help With Switch
20 Aug, 2007 - 08:50 AM
Post #6

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
You're welcome wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:31AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month