1

I'm trying to add user input into my class so that the user can input how much their package weighs and my class will tell them how much it will cost them. I am very new to java so I know this isn't amazing but it's the best I could do at this point. Thanks in advance

public class ShippingCosts {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int weight = 0;

        if (0 < weight && weight <= 1)
            System.out.println("The cost to ship the package is $3.50");
        if (1 < weight && weight <= 3)
            System.out.println("The cost to ship the package is $5.50");
        if (3 < weight && weight <= 10)
            System.out.println("The cost to ship the package is $9.50");
        if (10 < weight && weight <= 20)
            System.out.println("The cost to ship the package is $13.50");
        if (20 < weight)
            System.out.println("The package is too heavy to be shipped");

        System.out.println("How heavy is the package?");

    }

}
1
  • Please search Stack Overflow before posting. Commented Jul 10, 2016 at 3:09

1 Answer 1

1

simple example will be this: try and read more about it in the documentation Java Scanner

// read a number from System.in:
    Scanner scan = new Scanner(System.in);
    String s = scan.next();
    int i = scan.nextInt(); //read integers

> public String next()  --->it returns the next token from the scanner.

> public String nextLine()--->  it moves the scanner position to the next line and returns the value as a string. 

> public byte nextByte() --->it scans the next token as a byte. 

> public short nextShort() --->it scans the next token as a short value.

> public int nextInt() --->it scans the next token as an int value. 

> public long nextLong() --->it scans the next token as a long value. 

> public float nextFloat() --->it scans the next token as a float value. 

> public double nextDouble() --->it scans the next token as a double value.

prompting user for input:

Scanner input = new Scanner(System.in);

System.out.println("Please enter your name : "); //prompt user
s = input.next(); // getting a String value

System.out.println("Please enter your age : ");
i = input.nextInt(); // getting an integer

System.out.println("Please enter your salary : ");
d = input.nextDouble(); // getting a double
Sign up to request clarification or add additional context in comments.

2 Comments

So I added that into my code and imported java.util.Scanner, but I'm still really confused on how I can actually get from here to being able to get user input. When I said that I'm new to Java I mean like I started a few days ago so it's gonna take some time before I get better.
Thanks for the clarification!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.