3 Replies - 233 Views - Last Post: 07 December 2017 - 02:48 PM Rate Topic: -----

#1 radtech7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 license header, choose License Headers in Project Properties.
 * 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  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 GazinAtCode  Icon User is online

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 114
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3486
  • View blog
  • Posts: 15,763
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1