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

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(?, ?, ?, ?);   ...
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...
(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...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
Hello I need a small fix in my program. I need to display the youngest student...
Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...
I need to solve math problem by using freemat or mathlab program. However, I can't understand...
I need to solve math problem by using freemat or mathlab program. However, I can't understand how to make a code it. This is one example for code ( freemat ) format long f = @(x) (x^2) % define f(x) = x^2 a=0 b=1 N=23 dx = (b-a)/N % dx = delta_x 1. Methods: (a) Left Rectangular Rule also known as Lower sum. (b) Right Rectangular Rule also know as upper sum. (c) Midpoint Rule (d) Trapezoid Rule (e) Simpson...
I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT