# help with writing an expression multiplying ingredients

Page 1 of 1

## 3 Replies - 299 Views - Last Post: 07 December 2017 - 02:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408085&amp;s=c0fffdcd478544075e1a52b5281f6fe1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 1
• Joined: 07-December 17

# help with writing an expression multiplying ingredients

Posted 07 December 2017 - 08:29 AM

Hello,

Wondering if someone can help me with this code. I need to add number of cups and number of calories per cup. Then I need to display that as total calories.

When I run the code it shows total calories as null.

Thank you!
```/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SteppingStones;

import java.util.Scanner;
/**
*
* @author snhu.edu
*/
public class SteppingStone2_IngredientCalculator {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

/**
*Assign the following variables with the appropriate data type and value:

*VARIABLE NAME            VALUE

*nameOfIngredient         ""; //Name of Ingredient
*numberCups               0;  //Number of Cups
*numberCaloriesPerCup     0;  //Number of Calories per Cup
*totalCalories            0.0 //Total calories

*/

Scanner scnr = new Scanner(System.in);

System.out.println("Please enter the name of the ingredient: ");
String nameOfIngredient = scnr.next();

System.out.println("Please enter the number of cups of "
+ nameOfIngredient + " we'll need: ");
float numberCups = scnr.nextFloat();

System.out.println("Please enter the number of calories per cup: ");
int numberCaloriesPerCup = scnr.nextInt();
String totalCalories = null;

/**
* Write an expression that multiplies the number of cups
* by the Calories per cup.
* Assign this value to totalCalories
*/

System.out.println(nameOfIngredient + " uses " + numberCups
+ " cups and has " + totalCalories + " calories.");

}

}

```

This post has been edited by macosxnerd101: 07 December 2017 - 08:39 AM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: help with writing an expression multiplying ingredients

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12243
• Posts: 45,332
• Joined: 27-December 08

## Re: help with writing an expression multiplying ingredients

Posted 07 December 2017 - 08:43 AM

Look at line 43: String totalCalories = null;. This is why you are obtaining the specified output.

As a note- if you are performing numerical operations (like adding), you should use a numeric primitive data type rather than a String.

### #3 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## Re: help with writing an expression multiplying ingredients

Posted 07 December 2017 - 02:04 PM

Hello, and welcome to the forums.

You're not consistent when it comes to choosing data types. I think you should just use float all the way through.

Instead of that String variable you've set to null, you basically need: float totalCalories = numberCups * numberCaloriesPerCup;.

This post has been edited by GazinAtCode: 07 December 2017 - 02:06 PM

### #4 g00se

• D.I.C Lover

Reputation: 3517
• Posts: 15,923
• Joined: 20-September 08

## Re: help with writing an expression multiplying ingredients

Posted 07 December 2017 - 02:48 PM

There's really no compelling reason to use float over double unless you happen to be working in a very restricted environment