#No response.

20 messages · Page 1 of 1 (latest)

stable berry
#

I'm currently working on a simple program that takes user input to create a gorcery list with prices and item names. It's not giving me any error codes when I run but rather nothing is appearing. I'll attach a photo below.

heavy caveBOT
#

This post has been reserved for your question.

Hey @stable berry! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stable berry
wintry hill
#

how do you want to take item if your itemNames has 0 elements so size is 0

stable berry
#

so should i keep it at a set amount or ask the user for the amount of items beforehand and set that to the size?

wintry hill
#

u can even do while true, and if user wants to finish shopping he should write for example finish.

if (item.equals("finish")) break;
stable berry
#

Would this work?

#

I've tried running and am now getting a seperate problem

cunning osprey
#

it should be scanner.nextDouble() you're calling scanner.next()

stable berry
#

Sorry for not updating the screenshot, I did notice that and fixed it earlier

wintry hill
#

write this code here, on chat

white viperBOT
#

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


public class TotalPrice{
   public static void main(String[] args) {
       Scanner inpt = new Scanner(System.in);
       
       ArrayList<String> itemNames = new ArrayList<String>();
       ArrayList<Double> itemPrices = new ArrayList<Double>();

       for (int i = 0; i < itemNames.size(); i++) {
           String item = inpt.nextLine();
           System.out.println("Please type in the name of the item you would like to purchase: ");
           itemNames.add(item); //itemNames.add....system.in
           Double price = inpt.nextBoolean();
           System.out.println("Please type in the price of the item: ");
           itemPrices.add(price);
           if (item.equals("finish")) break;
           else {
               System.out.println("Error, please retype.");
           }
           
                 }
       }

       


          

       } ```

This message has been formatted automatically. You can disable this using /preferences.

wintry hill
#
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        ArrayList<String> itemNames = new ArrayList<>();
        ArrayList<Double> itemPrices = new ArrayList<>();

        while (true) {
            System.out.println("Please type in the name of the item you would like to purchase or finish: ");
            String item = scanner.nextLine();
            if (item.equals("finish")) {
                break;
            } else {
                itemNames.add(item);
                System.out.println("Please type in the price of the item: ");
                Double price = scanner.nextDouble();
                scanner.nextLine();
                itemPrices.add(price);
            }
        }
        System.out.println(itemNames);
        System.out.println(itemPrices);

    }
heavy caveBOT
#

If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.

royal lotus
heavy caveBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.