Question

In: Computer Science

My Java program keeps "running." I know I need to close a "loop" but I can't...

My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me.

import java.util.Scanner;
import java.util.ArrayList;


public class SteppingStone4_Loops {
  
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String recipeName = "";
ArrayList<String> ingredientList = new ArrayList();
String newIngredient = "";
boolean addMoreIngredients = true;

System.out.println("Please enter the recipe name: ");
recipeName = scnr.nextLine();

  
do {   
System.out.println("Would you like to enter an ingredient: (y or n)");
String reply = scnr.next().toLowerCase();

if (reply.equals("y")) {
System.out.println("Enter the name of the ingredient.");
newIngredient = scnr.next();   
ingredientList.add(newIngredient);   
} else if (!reply.equals("n")) {
System.out.println("Please enter y or n");
} else {
addMoreIngredients = false;
}   
  
} while (addMoreIngredients);

for (int i = 0; i < ingredientList.size(); i++) {
String ingredient = (String) ingredientList.get(i);
System.out.println(ingredient);

}
}
}

Solutions

Expert Solution

Updated Java Program :

import java.util.Scanner;
import java.util.ArrayList;


public class SteppingStone4_Loops {
  
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        String recipeName = "";
        ArrayList<String> ingredientList = new ArrayList<String>();
        String newIngredient = "";
        boolean addMoreIngredients = true;
        
        System.out.print("Please enter the recipe name: ");
        recipeName = scnr.nextLine();
        
          
        do {   
            System.out.print("Would you like to enter an ingredient: (y or n) : ");
            String reply = scnr.next().toLowerCase();
            
            if (reply.equals("y")) {
                System.out.print("Enter the name of the ingredient. : ");
                newIngredient = scnr.next();   
                ingredientList.add(newIngredient);   
            } else if (!reply.equals("n")) {
                System.out.println("Please enter y or n !!! ");
            } else {
                addMoreIngredients = false;
            }   
          
        } while (addMoreIngredients == true);
        
        System.out.println("\n***************************************\n");
        System.out.println("Recipe Name : " + recipeName);
        for (int i = 0; i < ingredientList.size(); i++) {
            String ingredient = (String) ingredientList.get(i);
            System.out.println("Ingredient " + (i+1) + " : " + ingredient);
            
        }
        System.out.println("\n***************************************");
    }
}

Output:

Thumbs Up Please !!!


Related Solutions

Java Counter Program I can't get my program to add the number of times a number...
Java Counter Program I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2. My Code: import java.util.Scanner; import java.util.Random;    import java.lang.*;       public class Dice    {               public static void main(String[] args)        {            Scanner in = new Scanner(System.in);            int Continue = 1;            //randomnum = new Random();           ...
I don't know why my java code is not running this error code pops up -->...
I don't know why my java code is not running this error code pops up --> Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll. Code below:    import java.util.Random;    import java.util.Scanner;    public class Assignment {    public static void combineStrings() {    String school = "Harvard";    String city = "Boston, MA";    int stringLen;       String upper, changeChar, combine;       stringLen = school.length();    System.out.println(school+" contains "+stringLen+" characters." );   ...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
My question is if n<r when there is no loop. I can't enter numbers again.Where is...
My question is if n<r when there is no loop. I can't enter numbers again.Where is the problem? How do it? #include int fact(int no) { if (no == 0) return 1; return no * fact(no - 1); } int calc(int n, int r,int *com,int *per) //as given in question formula of permutation n!/(n-r)! { *com=fact(n)/(fact(r)*fact(n-r)); *per=fact(n)/fact(n-r); } int main() { int n=0,r=0,co,pe; while(n<=0 || r<=0) { printf("Enter N value:"); scanf("%d",&n); printf("Enter R value:"); scanf("%d",&r); if(n { printf("N value should...
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
I need to create a monthly loan Calculator in C++. I know the formula but can't...
I need to create a monthly loan Calculator in C++. I know the formula but can't figure out what's wrong with my code. Any clarification would be appreciated! // Only add code where indicated by the comments. // Do not modify any other code. #include <iostream> #include <cmath> using namespace std; int main() {    // ---------------- Add code here --------------------    // -- Declare necessary variables here              --        int years = 0; //n     int LoanAmount =...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I need know how to Write a java program called character length that prompts the user...
I need know how to Write a java program called character length that prompts the user to enter a string and displays: The length of the string is: xxxxx The strings first character is: x    (where xxxxx is the length of the string and x is the first character.)
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
using java program I need only the tracing in detail, using my name "Nada Hamdan" Part-A:...
using java program I need only the tracing in detail, using my name "Nada Hamdan" Part-A: Consider first ten letters of your name and for each letter encode with an integer number. For example: If “Ahmed Naser” are the first ten letters from your name, then the array of integers will be {0, 7, 12, 4, 3,13,0,18,4,17}. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT