-2

Thank you for the suggestions this is my first time on stackoverflow as I am fairly new to programming. The problem that I am having is that my program doesn't ask for a name after the while loop is executed.

import java.util.*;
import java.lang.*;

/**
 * CS-12J
 * Sweetner.java
 * Purpose:
 *
 * @version 1.0 3/18/13
 */

public class Sweetner {

   /**
    * The main method begins the execution of the program
    *
    *@param args not used
    */

    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        String again = "yes";
        while(again.equals("yes")) {
            System.out.print("Enter the dieter's name: ");
            String name = input.nextLine();
            System.out.print("/n Enter the target weight of the dieter in pounds: ");
            double weightInPounds = input.nextDouble();

            //converts pounds to grams
            final double IBS_TO_GRAMS = 453.59;
            double weightInGrams = weightInPounds * IBS_TO_GRAMS;

            //finds lethal amout of sweetner for mouse
            double mouseWeight = 30;
            double lethalDoseMouse = 100;
            final double MOUSE_LETHAL_PROPORTION =
                (double) lethalDoseMouse / mouseWeight;

            //finds lethal amount of sweetner for human of given weight
            double lethalSweetner = MOUSE_LETHAL_PROPORTION * weightInGrams;

            //lethal number of cans
            final double SODA_SWEETNER = 0.001;
            final double SODA_GRAMS = 350;
            double lethalCans = lethalSweetner / (SODA_SWEETNER * SODA_GRAMS);

            //output
            System.out.println("For dieter: " + name);
            System.out.println("Dieter's target weight: " + weightInPounds
                + " pounds");
            System.out.println("Lethal dose in grams of sweetner is: "
                + lethalSweetner);
            System.out.println("Lethal number of cans of soda: "
                + lethalCans);

            //extra credit
            final int CANS_PER_DAY = 15;
            final double DAYS_IN_YEAR = 365.25;
            double yearsToLethal = lethalCans / (CANS_PER_DAY * DAYS_IN_YEAR);
            System.out.println("Years to lethal dose: " + yearsToLethal);
            System.out.println("Do you want to repeat the program? yes or no");
            again = input.next();
        }
    }
}
Scott P
  • 19
  • 3
  • 3
    I would opt against including my actual name (and the names of fellow students) in a forum like this ;-) – jahroy Mar 19 '13 at 01:52
  • 1
    Welcome to SO. Please read the [FAQ] and [Ask] for tips on writing good SO questions. Your question cannot consist of nothing but code. You must explain what you're having trouble with. – Jim Garrison Mar 19 '13 at 01:52
  • 1
    Please debug by yourself. Hint: adding code to the while loop bit by bit to see where is your logic error – Black Maggie Mar 19 '13 at 01:53
  • 1
    Not to be "that guy", but a year has 365.24 days. –  Mar 19 '13 at 01:57
  • I think this question will help you: http://stackoverflow.com/questions/5032356/using-scanner-nextline – duffymo Mar 19 '13 at 02:04
  • I disagree with everybody who closed this and didn't help. It was a misunderstanding about how to use Scanner that anyone new to Java could have trouble with. It didn't take much effort to run this class and see what the issue was. This is the guy's first question here. Not very welcoming. Shame on SO for being so quick to close. – duffymo Mar 19 '13 at 02:18
  • Well I've gone to the API and done my best will continue to try to figure it out. I hope it can be reopened so I can get help. – Scott P Mar 19 '13 at 02:24
  • I understood somewhat where you pointed me duffymo. But didn't quite understand how to properly use scanner.nextLine(); I thought that you had to declare it to a class such as double ____ = scanner.nextLine();. – Scott P Mar 19 '13 at 02:32
  • The code that I posted runs correctly as is. You need to read the link I posted in the earlier comment. You weren't consuming the newline character. The changed code that I posted does. Take what I posted, run it, verify that it meets your requirement, and then see what the differences are. – duffymo Mar 19 '13 at 02:57
  • You don't need more help, unless I misread your requirement. The altered code that I posted is correct. Did you even try it? – duffymo Mar 19 '13 at 02:58
  • It worked great thank you very much for your help! – Scott P Mar 19 '13 at 04:08

1 Answers1

0

Try this:

import java.util.*;
import java.lang.*;

/**
 * CS-12J
 * Sweetner.java
 * Purpose:
 *
 * @version 1.0 3/18/13
 */

public class Sweetener {

    /**
     * The main method begins the execution of the program
     *
     *@param args not used
     */

    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        String again = "yes";
        while(again.equals("yes")) {
            System.out.print("Enter the dieter's name: ");
            String name = input.nextLine();
            System.out.print("Enter the target weight of the dieter in pounds: ");
            double weightInPounds = Double.valueOf(input.nextLine());

            //converts pounds to grams
            final double IBS_TO_GRAMS = 453.59;
            double weightInGrams = weightInPounds * IBS_TO_GRAMS;

            //finds lethal amout of sweetner for mouse
            double mouseWeight = 30;
            double lethalDoseMouse = 100;
            final double MOUSE_LETHAL_PROPORTION =
                    (double) lethalDoseMouse / mouseWeight;

            //finds lethal amount of sweetner for human of given weight
            double lethalSweetner = MOUSE_LETHAL_PROPORTION * weightInGrams;

            //lethal number of cans
            final double SODA_SWEETNER = 0.001;
            final double SODA_GRAMS = 350;
            double lethalCans = lethalSweetner / (SODA_SWEETNER * SODA_GRAMS);

            //output
            System.out.println("For dieter: " + name);
            System.out.println("Dieter's target weight: " + weightInPounds
                    + " pounds");
            System.out.println("Lethal dose in grams of sweetner is: "
                    + lethalSweetner);
            System.out.println("Lethal number of cans of soda: "
                    + lethalCans);

            //extra credit
            final int CANS_PER_DAY = 15;
            final double DAYS_IN_YEAR = 365.25;
            double yearsToLethal = lethalCans / (CANS_PER_DAY * DAYS_IN_YEAR);
            System.out.println("Years to lethal dose: " + yearsToLethal);
            System.out.print("Do you want to repeat the program? yes or no");
            again = input.nextLine();
        }
    }
}
Scott P
  • 19
  • 3
duffymo
  • 293,097
  • 41
  • 348
  • 541