#Total price program
27 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @tropic locust! 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.
The size of itemNames is 0.
the value "i" is set to 0
this is the error code
Which is caused by?
according to it on line 13 which is "itemNames.add(inpt.next());" the scanner method is undefined
but I've defined my scanner object
Which is totally correct code.
So what's the problem here?
I don't know, the code on the screenshot wouldn't throw any compilation error, although it's noop.
You probably should add something to itemPrices?
Yes that’s for afterwards but as of right now I’m only trying to find a solution to my itemNames issue
When I entered all your code there are no issues. I don't have such an exception.
But I edited your code so it would work and let you enter items and prices.
Well I’m trying to have it so the user will input an answer and the answer will be placed into an array list which I will print later
I’m not sure if inpt.next is the right statement for such a thing
Then you have to think throught your stop condition.
I have it so if the user says stop the loop will break
💤 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.
@tropic locust itemsNames is Empty , you could try this
try {
Scanner input = new Scanner(System.in);
System.out.println("Please enter name of items you would like to purchase: ");
String item = input.nextLine();
System.out.println("Please enter the cost of item " + item + ":");
double itemPrice = input.nextDouble();
List<String> itemsNames = new ArrayList<>();
if (!item.isEmpty()) {
itemsNames.add(item);
}
List<Double> prices = new ArrayList<>();
// Read the item price
prices.add(itemPrice);
// Display the added items and prices
for (int i = 0; i < itemsNames.size(); i++) {
System.out.println("Item: " + itemsNames.get(i) + "\t Price: " + prices.get(i));
System.out.println("Yay, added successfully!");
}
input.close();
}catch (Exception e){
System.out.println(e.getMessage());
}
Please format your code to make it more readable.
For java, it should look like this:
````java
public void foo() {
}
````
Alright I’ll try it in a bit
this would be more accurate if you want to enter numbers with decimal point like 0.25 or something like that
try {
Scanner input = new Scanner(System.in);
input.useLocale(Locale.US);
System.out.println("Please enter name of items you would like to purchase: ");
String item = input.nextLine();
System.out.println("Please enter the cost of item " + item + ":");
double itemPrice = input.nextDouble();
List<String> itemsNames = new ArrayList<>();
if (!item.isEmpty()) {
itemsNames.add(item);
}
List<Double> prices = new ArrayList<>();
// Read the item price
prices.add(itemPrice);
// Display the added items and prices
for (int i = 0; i < itemsNames.size(); i++) {
System.out.println("Item: " + itemsNames.get(i) + "\t Price: " + prices.get(i));
System.out.println("Yay, added successfully!");
}
input.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Please format your code to make it more readable.
For java, it should look like this:
````java
public void foo() {
}
````