-2
import java.util.Scanner;

public class Formula {

public static void main(String[] args) {

    Scanner numIn = new Scanner(System.in);
    Scanner form = new Scanner(System.in);

    double r, d, h, a;
    String formula;

    System.out.println("Please state which circle formula you want to use:");
    System.out.println("Circumference");
    System.out.println("Area");
    System.out.println("Cylinder volume");

    formula = form.next();

    switch (formula) {
    case "Circumference":
        System.out.println("Please state the diameter: ");
        d = numIn.nextDouble();

        System.out.println("The circumference is:");
        System.out.println(3.14 * d);
        break;

    case "Area":
        System.out.println("Please state the radius: ");
        r = numIn.nextDouble();

        System.out.println("The area is:");
        System.out.println(3.14 * (r * r));
        break;

    case "Cylinder volume":
        System.out.println("State the area of the base: ");
        a = numIn.nextDouble();
        System.out.println("State the height of the cylinder: ");
        h = numIn.nextDouble();
        System.out.println("the volume is: ");
        System.out.println(a * h);
        break;

    default:
        System.out.println("Option not recognized");
        break;
    }
}

}

as you can see I am trying to create a formula calculator (note: I am only a begginer) and it all seemed to be working until the last 'case'. The last case "Cylinder volume" is not recognized when I type it in the console. All other cases work fine, and I do not see a difference between "Cylinder volume" and the other ones. Please help!

ViceroyFaust
  • 154
  • 1
  • 1
  • 9
  • 10
    use `form.nextLine()` instead of `form.next()` or you'll only get the first word ("Cylinder"). – Zircon Mar 13 '17 at 15:00
  • *"Why doesn't the code work"* is a bad title. It could apply to probably 90%+ of questions on SO. You might want to be more specific. – domsson Mar 13 '17 at 15:03
  • i guess you need to know the [difference between next() and nextLine()](http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class). And for the future need please [read the documentation](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) – Md Sifatul Islam Mar 13 '17 at 15:08
  • Pro Tip: Have the user enter just a menu-item number. Nobody wants to type "Cylinder volume". They want to type "3" ... – Fildor Mar 13 '17 at 15:08

3 Answers3

2

That is you used

 formula = form.next();

This only reads until end of the word but not the space so when you put "Cylinder volume" it reads Cylinder only.

It will work if you change it to

 formula = form.nextLine();
logger
  • 1,818
  • 3
  • 22
  • 44
0

Zircon has the solution. However, it may also be helpful to print in the default case what formula is set to:

   default:
        System.out.println("Option: " + formula + " not recognized");
        break;

Doing this kind of stuff will help your sanity in the future.

D. Wood
  • 56
  • 4
0

Try

Scanner form = new Scanner(System.in, "UTF-8").useDelimiter("\n");

Remember that Scanner doesn't work with non-ascii character.

Another test could be printing 'formula' before the switch, and check its content.