#No response.
20 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @stable berry! Please use
/closeor theClose Postbutton 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.
how do you want to take item if your itemNames has 0 elements so size is 0
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?
u can even do while true, and if user wants to finish shopping he should write for example finish.
if (item.equals("finish")) break;
look up how to take in a Double
it should be scanner.nextDouble() you're calling scanner.next()
Sorry for not updating the screenshot, I did notice that and fixed it earlier
write this code here, on chat
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.
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);
}
If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.
You'll also run into this problem
💤 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.