1
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

Tmadwh
  • 19
  • 2
  • 2
    Please [edit] the post and include the stack trace. --- Arrays are zero-indexed, i.e. fields of an array of size `n` can be accessed with indices `0, 1, 2, ..., n-1`. --- Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Nov 05 '21 at 20:10
  • 1
    "I don't know where" <- The error message will tell you exactly which line. See [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – OH GOD SPIDERS Nov 05 '21 at 20:12
  • @OHGODSPIDERS Sorry ,its this line" price[i] = Integer.parseInt(temp[i].substring(temp[i].indexOf("$")+1,temp[i].length())); " But I am so confused ,don't know how to edit it and where the error comes from.. – Tmadwh Nov 05 '21 at 20:13
  • 1
    Change `i<=count` to `i – OH GOD SPIDERS Nov 05 '21 at 20:15
  • @OHGODSPIDERS I have changed that,and it shows up nothing..When I type in something like Canada$12. – Tmadwh Nov 05 '21 at 20:17
  • That's because you initialize count as 0 and only update it for each comma you find. So any String entered without a comma will be handled as having a size 0 (not existing). "Test$1,Test2$2" would be treated as only having a count of 1... I guess you can see where I am going with this. Your logic to determine your count always ends up 1 short. After the user entered his list, check if he entered anything at all and if yes add 1 to your count for the first element that doesn't start with a comma. – OH GOD SPIDERS Nov 05 '21 at 20:28
  • @OHGODSPIDERS It works!I changed the i to 1 for the first for loop ,and it works well accompanying with setting i – Tmadwh Nov 05 '21 at 20:29
  • @OHGODSPIDERS BIG THANK TO YOU,finally works! – Tmadwh Nov 05 '21 at 20:30
  • @OHGODSPIDERS, consider converting your comment into an answer so that it can be accepted and ”closed” and others can benefit from it and easily understand which could be a possible solution for similar questions ;) Thanks! – João Dias Nov 06 '21 at 00:28

1 Answers1

0

It works with changing the i to 1 for the first for loop ,and it works well accompanying with setting i<count

Tmadwh
  • 19
  • 2