0

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. Specifically it seems to not execute this line after the loop. System.out.print("Enter the dieter's name: "); String name = input.nextLine(); Can someone please explain my possible miss use of the Scanner utility.

import java.util.*;

/**
 * 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("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
            final double MOUSE_WEIGHT = 30.0;
            final double LETHAL_DOSE = 100.0;
            final double MOUSE_LETHAL_PROPORTION =
                (double) LETHAL_DOSE / MOUSE_WEIGHT;

            //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\n\t\t");
            again = input.next();
        }
    }
}
RoneRackal
  • 1,190
  • 2
  • 8
  • 15
Scott P
  • 19
  • 3

5 Answers5

4

Use next() instead of nextLine() to get the name:

String name = input.next(); // to get the name

nextLine() scans the invisible newline character which seems to be the case when user enters yes. So if you want to avoid using next(), just put input.nextLine(); at the end of the loop to consume the newline character.

Sudhanshu Umalkar
  • 4,046
  • 1
  • 22
  • 33
  • I tried this and it prevents the user from having both a first and last name. – Scott P Mar 19 '13 at 03:03
  • Thank you very much I did not know you could just put input.nextLine(); without declaring it to something this fixed the problem! – Scott P Mar 19 '13 at 03:11
0

try this at the end of the loop:

again = input.nextLine();
  • makes the end of the program not execute properly. to where any key typed will close the program instead of looping. – Scott P Mar 19 '13 at 03:04
0

Change your

String name = input.nextLine();

to this and it'll work.

String name = input.next();
RainMaker
  • 41,717
  • 11
  • 78
  • 97
0

input.nextLine() retrieves the next newline character. In your System.out.print() you provided "\n" which triggered the nextLine() method.

System.out.print("Do you want to repeat the program? yes or no\n\t\t");
again = input.next();

You need another input.nextLine() right before the second statement, such that :

System.out.print("Do you want to repeat the program? yes or no\n\t\t");
input.nextLine();
again = input.next();

I hope this solves your problem. :)

Kim Honoridez
  • 676
  • 2
  • 11
  • 28
0

if the dieter's name may not be empty

System.out.print("Enter the dieter's name: ");
String name = scanLine.apply( input );

find scanLine here

Kaplan
  • 872
  • 3
  • 7