Cfs0004
Cfs0004

Reputation: 85

how to get user to pick inputs Java

Could someone explain how to do the if statement if the user picks the option for 2?

import java.util.Scanner;

public class PostalTest
{

    public static void main(String args []){

    Scanner type = new Scanner(System.in);

    String object;
    Double weighte, weightl;

    System.out.println("\nWhat type of package do you have?\n");
    System.out.println("\nMin Weight = 1oz. -- Max weight = 3.5oz for Letters 13 oz for Envelopes\n");
   System.out.println("(1) - Envelope");
   System.out.println("(2) - Letter");

   System.out.print("Please enter your selection:\t");
    object = type.nextLine();


        if (object.equals("1"))
            System.out.print("Enter a weight:\t");
            weighte = type.nextDouble();

            if (weighte > 13 )
                System.out.println("Too Big of a Package!");

            else if (weighte == 13)
                System.out.println("price = $3.62");

            else if (weighte >= 12 )
                System.out.println("price = $3.40");

            else if (weighte >= 11 )
                System.out.println("price = $3.18");

            else if (weighte >= 10 )
                System.out.println("price = $2.96");

            else if (weighte >= 9 )
                System.out.println("price = $2.74");

            else if (weighte >= 8 )
                System.out.println("price = $2.52");

            else if (weighte >= 7 )
                System.out.println("price = $2.30");

            else if (weighte >= 6 )
                System.out.println("price = $2.08");

            else if (weighte >= 5 )
                System.out.println("price = $1.86");

            else if (weighte >= 4 )
                System.out.println("price = $1.64");

            else if (weighte >= 3 )
                System.out.println("price = $1.42");

            else if (weighte >= 2 )
                System.out.println("price = $1.20");

            else if (weighte >= 1 )
                System.out.println("price = $.98");

            else if (weighte < 1 )
                System.out.println("Package is too small!");

            else
                System.out.println("Not a valid Package!");

        else if (object.equals("2"))    
                System.out.print("Enter a weight:\t");
                weightl = type.nextDouble();

            if (weightl > 3.5 )
                System.out.println("Too Big of a Package!");

            else if (weightl == 3.5)
                System.out.println("price = $1.15");

            else if (weightl >= 3 )
                System.out.println("price = $.93");

            else if (weightl >= 2 )
                System.out.println("price = $.71");

            else if (weightl >= 1 )
                System.out.println("price = $.71");

            else if (weightl < 1 )
                System.out.println("Package is too small!");


    }
}

Upvotes: 0

Views: 216

Answers (1)

Ankur Singhal
Ankur Singhal

Reputation: 26077

The Braces were missing for the parent if else, if (object.equals("1")) does not have any opening brace etc., now try this. I have highlighted the piece of code change for you.

public class PostalTest{
    public static void main(String args[]) {

        Scanner type = new Scanner(System.in);

        String object;
        Double weighte, weightl;

        System.out.println("\nWhat type of package do you have?\n");
        System.out.println("\nMin Weight = 1oz. -- Max weight = 3.5oz for Letters 13 oz for Envelopes\n");
        System.out.println("(1) - Envelope");
        System.out.println("(2) - Letter");

        System.out.print("Please enter your selection:\t");
        object = type.nextLine();

if (object.equals("1")) {

            System.out.print("Enter a weight:\t");
            weighte = type.nextDouble();

            if (weighte > 13)
                System.out.println("Too Big of a Package!");

            else if (weighte == 13)
                System.out.println("price = $3.62");

            else if (weighte >= 12)
                System.out.println("price = $3.40");

            else if (weighte >= 11)
                System.out.println("price = $3.18");

            else if (weighte >= 10)
                System.out.println("price = $2.96");

            else if (weighte >= 9)
                System.out.println("price = $2.74");

            else if (weighte >= 8)
                System.out.println("price = $2.52");

            else if (weighte >= 7)
                System.out.println("price = $2.30");

            else if (weighte >= 6)
                System.out.println("price = $2.08");

            else if (weighte >= 5)
                System.out.println("price = $1.86");

            else if (weighte >= 4)
                System.out.println("price = $1.64");

            else if (weighte >= 3)
                System.out.println("price = $1.42");

            else if (weighte >= 2)
                System.out.println("price = $1.20");

            else if (weighte >= 1)
                System.out.println("price = $.98");

            else if (weighte < 1)
                System.out.println("Package is too small!");

            else
                System.out.println("Not a valid Package!");

} else if (object.equals("2")) {

            System.out.print("Enter a weight:\t");
            weightl = type.nextDouble();

            if (weightl > 3.5)
                System.out.println("Too Big of a Package!");

            else if (weightl == 3.5)
                System.out.println("price = $1.15");

            else if (weightl >= 3)
                System.out.println("price = $.93");

            else if (weightl >= 2)
                System.out.println("price = $.71");

            else if (weightl >= 1)
                System.out.println("price = $.71");

            else if (weightl < 1)
                System.out.println("Package is too small!");
        }

}

}

Upvotes: 1

Related Questions