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...
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...
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...
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...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user decide the size of a 2d array then fill said array. #include <iostream> using namespace std; typedef int* IntArrayPtr; int main(void) { int column, row, i, j; IntArrayPtr *arr_num = new IntArrayPtr[row]; cout << " " << endl << " This program creates a 2D array of dynamic memory, fills it with user entered numbers, " << endl << " Then prints the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT