Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    I'm getting ArrayIndexOutOfBoundsException. Below is my code. Please help.


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Note.main(Note.java:10)


    Java Code:
    public class Note {
     
        public static void main(String[] args){
                String s = "Particular|Qty|Price\n" + "Bread|10|30\n" + "Milk|2|26";
                String []a = s.split("\n");
                int x = a.length;
                int []prod = {};
                for(int i=1 ; i<x ; i++){
                    String []b = a[i].split("\\|");
                    prod[i-1] = Integer.parseInt(b[1])*Integer.parseInt(b[2]);                //here's the problem
                }
                double total = 0; 
                for(int i=0 ; i<(x-1) ; i++){
                    total += prod[i];
                }
                double grand = 0;
                if(total<1000){
                    grand= total;
                }
                else if(total>=1000 && total<5000){
                    grand = (total * 0.95);
                }
                else{
                    grand = (total * 0.90);
                }
                System.out.println("| S.N |" + a[0] + "| Total |");
                for(int i=1 ; i<x ; i++){
                    System.out.println("| " + i + " |" + a[i] + "| " + prod[i-1] + " |");
                }
                System.out.println(grand);
     
        }
     
    }

  2. #2
    nirorg is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    i think that you have to change line 7 to:
    Java Code:
        int []prod = new int[x];

  3. #3
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    Quote Originally Posted by nirorg View Post
    i think that you have to change line 7 to:
    Java Code:
        int []prod = new int[x];
    Ty nirorg. Its working now.

Similar Threads

  1. Replies: 6
    Last Post: 04-27-2012, 12:37 AM
  2. Replies: 1
    Last Post: 02-27-2012, 09:11 PM
  3. Replies: 4
    Last Post: 01-04-2012, 11:23 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •