I am writing program that will eventually calculate the speed of sound though a medium using the switch function, but am unable to get it to read the numbers I input, here is the code if anyone can offer some pointers.

import javax.swing.JOptionPane;

/**
 *This program calculates the time it would take for sound to travel through one of three mediums.
 * Cpsc118 Computer programming 1
 * INSTRUCTOR   John Roberts
 * ASSIGNMENT    Assignment #3, question #2
 * @author       xxx,xxx
 * @version      July 20 2010
 */


public class speedofsound
{
public static void main(String[] args)
	{
	String input;
	int medium = 0;
	
	
JOptionPane.showMessageDialog (null, " Pick a medium : 1 for Air, 2 for Water and 3 for Steel");
String temp1=	JOptionPane.showInputDialog(null, "Enter 1, 2 or 3");	
				
switch (medium)
{

	case 1:
		System.out.println ( " You have chosen Air" );
		
	case 2:
		System.out.println ( " You have chosen Water");
		
	case 3:
		System.out.println (" You have chosen Steel");
		
	default:
		System.out.println ("Invalid choice");
		}
	
	

										
											
						

					}
		
	}

Edited 6 Years Ago by dylgod: n/a

You're switching on medium, which is 0. You're going to get the default case each time, unless you set medium to a new value, probably derived from user input.

Probably easier to extract the first character from the input string and switch on it as a char value, rather than going to the bother of converting it to an int.

That is,

switch (temp1.charAt(0) {
case '1': //air
case '2': //water
etc

If you want to turn the input into an int, you might think about using an array of strings:

String medium[] = {"Air", "Water", "Steel"};
(...) 
Sys.out.println("You have chosen "+medium[userInputAsInteger]);

I prefer that to a switch, if I'm just assigning a value to a variable.

// I have corrected your program as follows:
import javax.swing.*; 
public class speedofsound {
public static void main(String[] args) {
    String input;
    int medium = 0; 
medium = Integer.parseInt(  JOptionPane.showInputDialog(null, "Enter 1, 2 or 3 \nPick a medium : 1 for Air, 2 for Water and 3 for Steel")); 

switch (medium) {

    case 1:
        System.out.println ( " You have chosen Air" ); break;

    case 2:
        System.out.println ( " You have chosen Water");break;

    case 3:
        System.out.println (" You have chosen Steel");break;

    default:
        System.out.println ("Invalid choice");
        }
        }

}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Tong - that's right, the failure to assign to medium was causing the failure. This should run better. Well done.
However, since there's no calculation done with the value stored in medium, there's no reason to convert it to an int. You should get in the habit of avoiding unnecessary conversions, because they provide opportunities for error, and they slow down your code.
In this case, the parseInt() gives the user a chance to break the program by entering a non-integer value, which will throw a NumberFormatException. If you don't do the conversion, any bad value will print the error message, allowing you to exit gracefully or go back and let them try again.

Dear Jon,
Thank you very much for your comments.
Is the following modified program making sense in terms of your comments?
-----------------------

import javax.swing.*; 
public class speedofsound {
public static void main(String[] args) {
	String input;
	String medium;
medium=	new String (JOptionPane.showInputDialog(null, "Enter 1, 2 or 3 \nPick a medium : 1 for Air, 2 for Water and 3 for Steel"));	
char c = medium.charAt(0);		
switch (c) {

	case '1':
		System.out.println ( " You have chosen Air" ); break;
		
	case '2':
		System.out.println ( " You have chosen Water");break;
		
	case '3':
		System.out.println (" You have chosen Steel");break;
		
	default:
		System.out.println ("Invalid choice");
		}
		}		
}

Edited 6 Years Ago by tong1: n/a

What does the method showInputDialog used here return?
Does it need to be converted to a String?

The code should verify that the data entered was valid.
What would your code do if the user entered: "123"? It looks like it ignores the "23" part

The method showInputDialog used here returns an instance of the class String.
The input "123" is not valid.
The code is modified accordingly as follows:

import javax.swing.*; 
public class speedofsound {
public static void main(String[] args) {
	String input;	
String medium;
medium=	new String (JOptionPane.showInputDialog(null, "Enter 1, 2 or 3 \nPick a medium : 1 for Air, 2 for Water and 3 for Steel"));	
char c = (medium.length()==1)? medium.charAt(0) : 4;		
switch (c) {

	case '1':
		System.out.println ( " You have chosen Air" ); break;
		
	case '2':
		System.out.println ( " You have chosen Water");break;
		
	case '3':
		System.out.println (" You have chosen Steel");break;
		
	default:
		System.out.println ("Invalid choice");
		}
		}		
}

Why create a new String from an existing Sring?
medium= new String (


How about showing what the invalid choice is:
System.out.println ("Invalid choice: " + medium);

Dear NormR, you are right. The program has been modified accordingly as follows:

import javax.swing.*; 
public class speedofsound {
public static void main(String[] args) {
String medium=JOptionPane.showInputDialog(null, "Enter 1, 2 or 3 \nPick a medium : 1 for Air, 2 for Water and 3 for Steel");
char c = (medium.length()==1)? medium.charAt(0) : 4;		
switch (c) {

	case '1':
		System.out.println ( " You have chosen Air" ); break;
		
	case '2':
		System.out.println ( " You have chosen Water");break;
		
	case '3':
		System.out.println (" You have chosen Steel");break;
		
	default:
		System.out.println ("Invalid choice: " + medium);
		}
		}		
}

Edited 6 Years Ago by tong1: n/a

Looks good from here. The only thing I'd add is that you might want to send someone back to the choice if they give a bad input. Probably the most intuitive way to do this would be to have a while(!done) loop around this block of code and set done=false if they make a bad choice. Obviously, you want to initialize done to true in this case.

Thank you for your advise. I have modified the code accordingly.

import javax.swing.*; 

public class speedofsound {
public static void main(String[] args) {
boolean pass=false;
	while(true){
String medium=JOptionPane.showInputDialog(null, "Enter 1, 2 or 3 \nPick a medium : 1 for Air, 2 for Water and 3 for Steel");
char c = (medium.length()==1)? medium.charAt(0) : 4;		
switch (c) {

	case '1':
		System.out.println ( " You have chosen Air" ); 
		pass = true;
		break;
		
	case '2':
		System.out.println ( " You have chosen Water");
		pass = true;
		break;
		
	case '3':
		System.out.println (" You have chosen Steel");
		pass = true;
		break;
		
	default:
		System.out.println ("Invalid choice: " + medium);
		}
		if (pass)
		break;
		}
	System.out.println("Bey for now.");		
	}
}
This article has been dead for over six months. Start a new discussion instead.