1

I have written this code, however, every time I input a decimal value, it doesn't work. How can I make this code work even if I input a decimal value? For example, if I input a value of 7.5, it should display that "the shipping cost is $9.45"

    import java.util.Scanner;

public class IfElse {
  public static void main(String[] args) {
    int marksObtained;

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter a package weight in pounds:");

    marksObtained = input.nextInt();

    if (marksObtained>20)
        {
            System.out.println("The package is too heavy to be shipped");
        }
        else if (marksObtained>10)
        {
            System.out.println("The shipping cost is $12.50");
        }
            else if (marksObtained>3)
        {
            System.out.println("The shipping cost is $9.45");
        }
            else if (marksObtained>1)
        {
            System.out.println("The shipping cost is $4.95");
        }
            else if (marksObtained>0)
        {
            System.out.println("The shipping cost is $2.95");
        }
        else if (marksObtained<0)
        {
            System.out.println("The weight must be greater than zero");
        }
  }
}
grif389
  • 11
  • 1

4 Answers4

1

You can use nextFloat or nextDouble

Scanner s = new Scanner (System.in);
float a = s.nextFloat ();
System.out.println(a);

Using nextInt will expect an int value to be entered and will throw a java.util.InputMismatchException if a int is not entered

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

Look at the code that you use to read input:

int marksObtained;`enter code here`
marksObtained = input.nextInt();

The key here is to understand that an int can only represent whole number values, not decimals. For decimals, you need to use a double or a float. For example:

double marksObtained = input.nextDouble();

I suggest you go back and review the basic data types which Java supports. You should also familiarize yourself with the documentation for the Scanner class as well as the rest of the documentation for the standard Java APIs.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

nextInt() only works for integers. Use nextDouble()

robbieperry22
  • 1,753
  • 1
  • 18
  • 49
1

Use nextDouble method as below

public static void main(String[] args) {
    double marksObtained;

    System.out.println("Please enter a package weight in pounds:");
    Scanner input = new Scanner(System.in);
    marksObtained = input.nextDouble();
    input.close();

    if (marksObtained > 20) {
        System.out.println("The package is too heavy to be shipped");
    } else if (marksObtained > 10) {
        System.out.println("The shipping cost is $12.50");
    } else if (marksObtained > 3) {
        System.out.println("The shipping cost is $9.45");
    } else if (marksObtained > 1) {
        System.out.println("The shipping cost is $4.95");
    } else if (marksObtained > 0) {
        System.out.println("The shipping cost is $2.95");
    } else if (marksObtained < 0) {
        System.out.println("The weight must be greater than zero");
    }
}

And close the scanner, it is good practice doing it.

Rmahajan
  • 1,311
  • 1
  • 14
  • 23