0

I am new to programming and I have some error. I get this error as I build my code in Java, it has to do with the int and double. Here is my code:

import java.util.Scanner;
import java.text.DecimalFormat;

public class Overloader 
{
    public static void main(String[] args) 
    {
        Scanner k1 = new Scanner(System.in);
        double selection = 0, radius, height;

        do 
        {
            System.out.println("Select the options from [1-3]");
            System.out.println("1- Volume of the Cylinder");
            System.out.println("2- Volume of the Sphere");
            System.out.println("3- Exit");
            selection = k1.nextDouble();

            switch (selection) 
            {
                case 1:
                    System.out.print("Enter radius: ");
                    radius = k1.nextDouble();
                    System.out.print("Enter height: ");
                    height = k1.nextDouble();
                    System.out.printf("Volume = %10.3f%n", getVolume(radius, height));
                    break;
                case 2:
                    System.out.print("Enter radius: ");
                    radius = k1.nextDouble();
                    System.out.printf("Volume = %10.3f%n", getVolume(radius));
                break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("Incorr... choice!");
            } // end switch
        } while (selection != 3);
    }

    public static double getVolume(double radius, double height) 
    {
        return Math.PI * radius * radius * height;
    }

    public static double getVolume(double radius) 
    {
        return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
    }
}

The debugger says:

switch (selection) {
        ^
  required: int
  found:    double
1 error
Process completed.

Please help, I'd appreciate it. Thank you very much.

Thomas
  • 6,291
  • 6
  • 40
  • 69
  • 2
    You have a **compiler** error there. A basic skill is to know to what program you're talking or what program is talking to you. – Ingo May 05 '14 at 12:22
  • Have a look at [The switch Statement](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html). – Braj May 05 '14 at 12:25
  • And the programmer invented "indentation" :) – Joffrey May 05 '14 at 12:29

2 Answers2

3

A switch cannot be based on a double or a float. According to the data range, you probably need to replace

selection = k1.nextDouble();

with

selection = k1.nextInt;

(and selection be declared as an int, assuggested by Ingo)

chburd
  • 4,131
  • 28
  • 33
1

You can't switch on a double. You can refer to the language specification #14.11:

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.

In your case you expect an int so simply use:

int selection = k1.nextInt();
assylias
  • 321,522
  • 82
  • 660
  • 783