-7

How can I change this code to make variable operation2 working in second switch function. When I do parseDouble it gets error bla bla parsedouble.
It doesn't work even if I try to create global operation2.

package matura;

import java.util.Scanner;

public class MeasureUnits {

public static void main(String args[])

{
    String operation;
    String operation2;

    Scanner value = new Scanner(System.in);
    System.out.println(" Write value: :");

    double wartość;
    wartość = value.nextDouble();

    double yard, inch, foot, mile;
    double metr, decymetr, centymetr, kilometr;

    centymetr = 1;

    metr = 100 * centymetr;
    centymetr = 1;
    kilometr = centymetr * 100000;
    decymetr = 10 * centymetr;

    yard = 0.9144 * metr;
    inch = 2.54 * centymetr;
    mile = 1.609344 * kilometr;
    foot = 30.48 * centymetr;

    Scanner input1 = new Scanner(System.in);
    System.out.println(" Choose one of measure units" + " 'yard' 'inch' 'feet' 'mile' :");
    operation2 = input1.next();

    switch (operation2) {
    case "yard":
        System.out.println("You've choosed 'yard' ");
        break;
    case "inch":
        System.out.println(" You've choosed 'inch'");
        break;

    case "mile":
        System.out.println(" You've choosed 'mile'");
        break;

    case "foot":
        System.out.println("You've choosed 'foot'");
        break;

    }

    {

        Scanner input2 = new Scanner(System.in);
        System.out.println(" Choose one of polish measure units" + " 'metr' 'centymetr' 'decymetr' 'kilometr' :");
        operation = input2.next();

        switch (operation) {
        case "metr":
            System.out.println(operation2 * wartość / 100);
            break;

        case "centymetr":
            System.out.println(operation2 * wartość / 100);
            break;

        case "kilometr":
            System.out.println(operation2 * wartość / 100000);
            break;

        case "decymetr":
            System.out.println(operation2 * wartość / 10);
            break;

        }

    }
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Skillzone
  • 1
  • 3
  • 7
    `bla bla parsedouble` is called an error message. Some talented programmer spent a long time working on a compiler so that it would give you *meaningful* and *useful* error messages. Please learn that they are essential in debugging. So "blah blah" doesn't help anyone. Also, `switch` is not a function, and you can't `switch` on a double. See the exact same question here: http://stackoverflow.com/questions/5141830/switch-expression-cant-be-float-double-or-boolean – Kon Aug 05 '15 at 17:54
  • In addition to all of the above, I find it hard to believe your actual code is formatted like that. Can you neaten it up so my eyes don't hurt? – dcsohl Aug 05 '15 at 18:00
  • `bla bla parsedouble` Just another *pesky* error message telling you exactly what is wrong and where it's happening. Hint: You should read it and if you don't understand what it is and why it's happening, do a cursory Google search and make an effort to rectify that. If you're *still* stuck, then you'd be in a good place to ask here. – tnw Aug 05 '15 at 18:16
  • I changed double to int, but don't know where to put it... if in first switch I get error after english measure units etc. Exception in thread "main" java.lang.NumberFormatException: For input string: "yard" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at matura.MeasureUnits.main(MeasureUnits.java:39) Java Result: 1 – Skillzone Aug 05 '15 at 18:18

1 Answers1

0

Did you have a look at JSR 363 and the Java Units of Measurement Project: http://unitsofmeasurement.github.io/

The Reference Implementation and other unit systems (SI, US, etc.) contain all relevant unit definitions in Java and if you need more, you can define your own on top of the API.

Werner Keil
  • 592
  • 5
  • 12