import java.util.Scanner;
public class Question1 {
public static void main(String[] args){
System.out.println(" Welcome to Shopping");
System.out.println("Please enter your shopping (item$price and seperate by comma): ");
Scanner scanner = new Scanner(System.in);
String list = scanner.next();
//here is my code
System.out.println("Here is the list of items:");
int p = 0;
int count = 0;
for(int i = 0; i < list.length(); i++) {
if(list.charAt(i) == ',')
count++;
}
int[] price = new int[count];
String[] temp = list.split("\\,");
for(int i=0;i<=count;i++){
int a = i+1;
**price[i] = Integer.parseInt(temp[i].substring(temp[i].indexOf("$")+1,temp[i].length()));**
p+=price[i];
System.out.println("No."+a+" "+temp[i].substring(0,temp[i].indexOf("$")));
}
System.out.println("The total price is: $"+p+".");
System.out.println("Thank you for using this program!!");
}
}
}
//I dont know why it couldnt print out,and shows something like arrary out of ...
I try to print out something like Type nameoftheitem$price,nextone.It will looks like mango$12, tomato$14, print out like 1.mango 2.tomato
at the end,print out its sum cost 26