Jump to content
Search In
• More options...
Find results that contain...
Find results in...

# Using Java to create a calculator that calculates total weight and price of rice

Go to solution Solved by guitargirl15,

1. You don't need the if(x==0) check, because if x==0 it will go to the top of the while loop, see that x != 0 is not true, and stop.

2. Make the Scanner object before displaying any text

3. Before asking for the weight of rice per bag, ask for the price per kilogram. Store the result in a variable (double price = keyboard.nextDouble())

4. Use `price` to calculate your final price at the end

I made a code that lets you input numbers and only stop when you input zero and it adds every number that you input. I want to add another scanner input so that i can input the price per kilogram and then multiply it to the total weight when the loop is done but I can't figure out a way to do it. Please help me guys

This is my code:

int x = 0;
int sum = 0;

System.out.println("Enter the weight of rice per bag:");
System.out.print("Number: ");
Scanner keyboard = new Scanner(System.in);
x = keyboard.nextInt();
while (x != 0) {
sum = x + sum;
System.out.println("The total so far is " + sum + ".");
System.out.print("Number: ");
x = keyboard.nextInt();
if (x == 0) {
break;
}
}
System.out.println("\nThe total is " + sum + ".");
}
}

##### Share on other sites

1. You don't need the if(x==0) check, because if x==0 it will go to the top of the while loop, see that x != 0 is not true, and stop.

2. Make the Scanner object before displaying any text

3. Before asking for the weight of rice per bag, ask for the price per kilogram. Store the result in a variable (double price = keyboard.nextDouble())

4. Use `price` to calculate your final price at the end

##### Share on other sites
```int x = 0;
int sum = 0;

System.out.println("Enter the weight of rice per bag:");
System.out.print("Number: ");
Scanner keyboard = new Scanner(System.in);
x = -1;
while (x != 0 ) {
if (x>0) {
sum = x + sum;
System.out.println("The total so far is " + sum + ".");
}

System.out.print("Number (0 to stop): ");
x = keyboard.nextInt();
if (x == 0) {
break;
}
}
System.out.println("\nThe total is " + sum + ".");
}
}```

Note that integers can be negative... I think, unless int in java is unsigned.. See in the code above how I'm only adding to the sum numbers that are bigger than 0, which allows me to only write once the messages in the loop. Also seems like there's a few extra brackets in my code... and yours.

edit: so basically I'm giving x a value that's not 0, because I want to go in the loop that follows at least once. So I chose -1 but any negative value would work. In the loop only positive numbers are added to the sum, so that bit where value is added to sum and message with total shown on screen is skipped, and the code moves on asking for next (first) number.

After the while loop you can print a message saying "now tell me the price per kg " or whatever  and read an integer or double and put it in another variable (for example y)  and then do the multiplication and show the result on screen with another println

##### Share on other sites
5 minutes ago, mariushm said:

Note that integers can be negative... I think, unless int in java is unsigned.

Java has no unsigned integer type(s), so yes you'd need to explicitly check for negative numbers.

Remember to quote or @mention others, so they are notified of your reply

##### Share on other sites

Thank you very much for your help guys! I love this community

##### Share on other sites
13 minutes ago, mariushm said:
```
int x = 0;
int sum = 0;

System.out.println("Enter the weight of rice per bag:");
System.out.print("Number: ");
Scanner keyboard = new Scanner(System.in);
x = -1;
while (x > 0 ) {
if (x>0) {
sum = x + sum;
System.out.println("The total so far is " + sum + ".");
}

System.out.print("Number (0 to stop): ");
x = keyboard.nextInt();
if (x == 0) {
break;
}
}
System.out.println("\nThe total is " + sum + ".");
}
}```

Note that integers can be negative... I think, unless int in java is unsigned.. See in the code above how I'm only adding to the sum numbers that are bigger than 0, which allows me to only write once the messages in the loop. Also seems like there's a few extra brackets in my code... and yours.

After the while loop you can print a message saying "now tell me the price per kg " or whatever  and read an integer or double and put it in another variable (for example y)  and then do the multiplication and show the result on screen with another println

There are a few issues with this code:

1. You will never enter the while loop.  x == -1, so  x > 0 is false, and it will never start

2. Assuming it does enter the while loop, you don't need to do if (x>0): you know that x > 0 because your while loop just checked for that condition.

3. You check if ( x == 0) again at the end, which you don't need to do. if you don't check, your while loop won't loop again anyways, because if x == 0, it won't satisfy the while loop's condition

##### Share on other sites
11 minutes ago, guitargirl15 said:

There are a few issues with this code:

1. You will never enter the while loop.  x == -1, so  x > 0 is false, and it will never start

2. Assuming it does enter the while loop, you don't need to do if (x>0): you know that x > 0 because your while loop just checked for that condition.

3. You check if ( x == 0) again at the end, which you don't need to do. if you don't check, your while loop won't loop again anyways, because if x == 0, it won't satisfy the while loop's condition

1. Yeah, you're right, I meant to say !=0 in the while loop  and then in the if , to use  >0

2.  No,  because first time in the loop  x is -1, and I don't want to add -1 to the sum, I also want to ignore any negative number entered by user (because the scanner  will accept negative numbers)

3.   Yeah, you're right, if the while (x!=0) is used for the loop

I went back and edited the code just in case he copy pastes the code.

Thank you for the observations...

##### Share on other sites

Here's a way to enter two numbers using the same scanner then output the multiplied results:

```import java.util.Scanner;

public class Main {

public static void main(String[] args) {
final Scanner keyboard = new Scanner(System.in);
System.out.println("Enter zero or a negative number to quit");

while(true) {
System.out.print("Price of rice [€/kg]: ");
final float pricePerWeight = keyboard.nextFloat();
if (pricePerWeight <= 0) {
return;
}

System.out.print("Total weight [kg]: ");
final float weight = keyboard.nextFloat();
if (weight <= 0) {
return;
}

final float price = weight * pricePerWeight;
System.out.printf("Total price: %,.2f€%n", price);
}
}
}```

This gives you output like this:

```Enter zero or a negative number to quit
Price of rice [€/kg]: 10
Total weight [kg]: 100
Total price: 1,000.00€
Price of rice [€/kg]: 2.3
Total weight [kg]: 1000
Total price: 2,300.00€
Price of rice [€/kg]: 0
```

Remember to quote or @mention others, so they are notified of your reply

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×