1

I need get users number of items, the prices, and print the prices like a receipt. Next I have to print out the subtotal, tax = .07 and the total.

I have tried using a for loop to get the info to add in the array. I have now tried the while loop.

Scanner sc = new Scanner(System.in);
     double SubTot = 0;

     //double numItmes = 0;
     int numItems = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of items: "));
     double[] items = new double[numItems];
     items[numItems] =  Double.parseDouble(JOptionPane.showInputDialog("Enter the prices: "));
     double i = 0;
         while(i<numItems){
             items[numItems]= sc.nextInt();
             i++;
         }
        System.out.println("Here is the current list: \n" + Arrays.toString(items));

        // double total = 0;
         //double tax = 0.07;

     JOptionPane.showMessageDialog(null,"The Sub Total is: " + SubTot + "The Tax is" + tax + "\n The Totoal is" + total);

}

>

My problem is getting the added array to spit out in consecutive lines. Also my tax is not being added to my total, or being multiplied by the subtotal. It keep posting the subtotal as 0 tax as .07 and total 0. What am I supposed to do?

*first time Java User. New programmer. Constructive criticism welcome

1 Answers1

0

Welcome to Stackoverflow! Very nice that you are starting to code - it's an awesome skill to have.

So, an ArrayIndexOutOfBounds Exception occurs when you try to access indexes of the Array that don't exist. When you put the Number (-> Double) "10" into an Array and nothing more, your Array looks like this: [10] It does not put 10 Elements in the Array. Also, Arrays are zero-based. That means, that the first element's index is 0, the 2nd's index is 1 and so on. When having an array of the size 10, the highest index is 9.

The For and While Loop do pretty much the same thing.

On a sidenote: While Stackoverflow is for resolving code-problems, searching existing entries on the problem is encouraged and often faster for yourself. It's a good start to just google the Exception's name (ArrayIndexOutOfBounds) :)

EDIT:

To clarify this:

double[] items = new double[5];

This code will create an Array of the size five. items[5] will still throw an ArrayIndexOutOfBounds Exception because Arrays are nullbased (which you try in the while loop). If you want to loop through the array, try items[i] - the i-value changes with every iteration by 1 (i++).

Meiswjn
  • 752
  • 6
  • 17
  • 1
    Thank you, I realize there are a tone of other problems that have some of the same issues that my program is having. The thing is, I have been trying to solve this programming issue for almost a week now. I have googled. I have watched youtube videos and even have gone to Linda.com to try and solve this problem on my own. With that being said, there are tons of topics around this issue. I have googled the error that I was getting and it is hard to apply it to my situation because there is so much! I appreciate you adding the code help. I will apply What you have shown. – Monica Breitweiser Aug 15 '19 at 01:56