-3

I'm making a program to calculate the speed of sound through a certain material. Here is the code:

  switch (user_Choice)
 {
case 1:
  System.out.println("The sound wave took " + speed.getSpeedInAir() + " seconds to travel through " +
                     distance + " feet of Air.");
  break;

case 2:
  System.out.println("The sound wave took " + speed.getSpeedInWater() + " seconds to travel through " +
                     distance + " feet of Water.");
  break;

case 3:
  System.out.println("The sound wave took " + speed.getSpeedInSteel() + " seconds to travel through " +
                     distance + " feet of Steel.");
  break;

default:
  System.out.println("Not a valid input.");}

I don't know why this isn't working. Can I not have that many "+" signs per case?

EDIT: Here is the user_Choice input

System.out.println("Pick a medium (1, 2, or 3):\n1. Air\n2. Water\n3. Steel");
user_Choice = kbReader.nextInt();
System.out.print("Enter the distance the sound wave traveled in feet: ");
distance = kbReader.nextDouble();
SpeedOfSound speed = new SpeedOfSound(distance);
  • Specify what does `isn't working` means? How it should work? – Eypros Nov 04 '14 at 14:19
  • It always shows the default option? How are you reading user_Choice var? – AndreDuarte Nov 04 '14 at 14:20
  • What is `user_Choice`? How does it get its value? – EpicPandaForce Nov 04 '14 at 14:21
  • We need more information as to what it is doing. Does it produce an error? Does it compile? If it doesn't what is the stack trace? I don't see anything in this snippet that is syntactically incorrect, and you can have just about anything in a case statement (whole program executions even). – Marshall Tigerus Nov 04 '14 at 14:22
  • post the code showing `user_Choice` input – Johny Nov 04 '14 at 14:22
  • Default is selected if your case is not among 3 choices provided 1, 2 and 3 – Crawler Nov 04 '14 at 14:22
  • Sorry for not being more specific... It only shows the default one. It should use the methods in another class to get the speed in that material. user_Choice is used as an Int. I have a menu that basically says, "Pick a material (1-3)"... – Dalton Craig Nov 04 '14 at 14:23
  • If your `user_Choice` is a integer variable of value between 1 to 3 it will enter the switch – Santhosh Nov 04 '14 at 14:28

1 Answers1

0

If kbReader is a Scanner remember that nextInt() and nextDouble() need a nextLine() in order to get rid of the \n. Calling nextDouble() after nextInt() will give to distance the value null.

A workaround is to add a nextLine() after the first nextInt():

System.out.println("Pick a medium (1, 2, or 3):\n1. Air\n2. Water\n3. Steel");
user_Choice = kbReader.nextInt();
kbReader.nextLine(); //Consumes the \n (new line) character.
System.out.print("Enter the distance the sound wave traveled in feet: ");
distance = kbReader.nextDouble();
SpeedOfSound speed = new SpeedOfSound(distance);

If before your user_Choice = kbReader.nextInt(); you are performing another nextInt() or similar, the switch will always choose the default value, since user_Choice is null.

Community
  • 1
  • 1
Narmer
  • 1,386
  • 6
  • 16