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

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




Switch Statement Program

 
Reply to this topicStart new topic

Switch Statement Program

shivers20
28 May, 2007 - 12:21 PM
Post #1

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 14


My Contributions
Write a java program that displays a student's status based on the following codes:

Code Student Status
1 Freshman
2 Sophmore
3 Junior
4 Senior
5 Masters Program
6 Doctoral Program

Your program should accept the code number as a user-entered inout value and based on this value display the correct student status. If an incorrect code is entered, your program should display the string "An incorrect code was entered".


CODE

import.javax.swing.*:

public class Status
{
     public static void main(String[] args)
     {
         String s1, status;
         int code;

         s1 = JOptionPane.showInputDialog("Enter a number:");
         code = Integer.parseInt(s1);
        
         switch (code)
         {
            case 1:
               status = "Freshman";
               break;

            case 2:
               status = "Sophomore";
               break;

             case 3:
               status = "Junior";
               break;

             case 4:
                status = "Senior";
                break;

             case 5:
                status = "Masters Program";
                break;

             case 6;
                status = Doctoral Program";
                break;

             default:
                 status = "An incorrect code was entered";
             }
            
              JoptionPane.showMessageDialog(null, status, "Program", JOptionPane.INFORMATION_MESSAGE);

             System.exit(0);
      }
}



User is offlineProfile CardPM
+Quote Post

keems21
RE: Switch Statement Program
28 May, 2007 - 12:25 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
And what would be your problem and/or question?
User is offlineProfile CardPM
+Quote Post

shivers20
RE: Switch Statement Program
28 May, 2007 - 02:44 PM
Post #3

New D.I.C Head
*

Joined: 27 May, 2007
Posts: 14


My Contributions
QUOTE(keems21 @ 28 May, 2007 - 01:25 PM) *

And what would be your problem and/or question?



I would like to know if the program is correct. Thankyou
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Switch Statement Program
28 May, 2007 - 04:40 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Your program had a little errors mostly wrong tokens.
I modified your code and works ok.
But first I want to say where the errors were.

QUOTE
import.javax.swing.*:

should be
CODE
import javax.swing.*:


QUOTE
case 6;
status = Doctoral Program";
break;

should be
CODE
case 6:
                status = "Doctoral Program";
                break;


and
QUOTE
JoptionPane.showMessageDialog(null, status, "Program", JOptionPane.INFORMATION_MESSAGE);

should be
CODE
JOptionPane.showMessageDialog(null, status, "Program", JOptionPane.INFORMATION_MESSAGE);


just small errors.

Here is the code that works:

CODE
import javax.swing.*;

public class Status
{
     public static void main(String[] args)
     {
         String s1, status;
         int code;

         s1 = JOptionPane.showInputDialog("Enter a number:");
         code = Integer.parseInt(s1);
        
         switch (code)
         {
            case 1:
               status = "Freshman";
               break;

            case 2:
               status = "Sophomore";
               break;

             case 3:
               status = "Junior";
               break;

             case 4:
                status = "Senior";
                break;

             case 5:
                status = "Masters Program";
                break;

             case 6:
                status = "Doctoral Program";
                break;

             default:
                 status = "An incorrect code was entered";
             }
            
              JOptionPane.showMessageDialog(null, status, "Program", JOptionPane.INFORMATION_MESSAGE);

             System.exit(0);
      }
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:57PM

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