Results 1 to 4 of 4
  1. #1
    Majikk is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Unhappy NEED HELP.. java.lang.ArrayIndexOutOfBoundsException

    I'm working with a program that has to input numbers using array and methods...
    When I compile the program errors do not appear, however the java.lang.ArrayIndexOutOfBoundsException shows up when I run it. What's wrong with my code?

    MAIN
    ================================================== =================
    package seventeento25;
    import java.util.Scanner;


    public class Ex25 {
    public static void main(String[] args){

    meth25 methodko=new meth25();
    Scanner sc=new Scanner(System.in);
    int[] num=new int[3];


    for(int i=0;i<=num.length-1;i++){

    System.out.print("Enter a number: ");
    num[i]=sc.nextInt();
    }

    for(int c=0;c<=num.length-1;c++){
    methodko.setNum(num[c]);
    }

    methodko.setLength(num.length);
    methodko.show();
    }}
    ================================================== =========================================



    METHODS CLASS
    ================================================== =======================
    package seventeento25;

    public class meth25 {
    private int wa;

    public void setLength(int o){

    wa=o;
    }

    private int[] number=new int[wa];

    public void setNum(int numm){

    number[wa]=numm;
    }

    public int getNum(){

    return number[wa];
    }

    public void show(){


    for(int b=0;b<=number.length-1;b++){

    System.out.print(number[b]+" ");

    }


    }


    }

  2. #2
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: NEED HELP.. java.lang.ArrayIndexOutOfBoundsException

    i have made a lot of changes, but I believe this is the outcome you want. Basically, I put the scanner into the method class and cleaned it up a little. I hope this helps and let me know if you have any questions.

    Java Code:
     
    import java.util.Scanner;
    
    
    public class Ex25  
    {
        public static void main(String[] args){
        int[] num = new int[3];
        meth25 methodko = new meth25(num);
    
    
    
        methodko.setNum();
    
    
    //methodko.setLength(num.length);
    methodko.show();
    }
    }

    Java Code:
    import java.util.Scanner;
    public class meth25 
    {
        private int wa;
        private int[] number = new int[3];
    
        public meth25(int[] num)
        {
            number = num;
        }
    
        public void setNum()
        {
            Scanner sc = new Scanner(System.in);
    
            for(int i = 0; i < number.length;i++)
            {
                System.out.print("Enter a number: ");
                number[i] = sc.nextInt();
            }
        }
    
        public int getNum()
        {
            return number[wa];
        }
    
        public void show()
        {
            for(int b=0;b<=number.length-1;b++)
            {
                System.out.print(number[b] + " ");
            }
        }
    }

  3. #3
    Majikk is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: NEED HELP.. java.lang.ArrayIndexOutOfBoundsException

    hey thanks.. The output from your code is exactly what I'm looking for. However my goal is to put the "for loops" and Scanner on the on the main class.
    I want the final output to come from the method class.

    Did you try run my code?.. Was there any errors or just Exceptions?.. I'm confused with this java.lang.ArrayIndexOutOfBoundsException
    Last edited by Majikk; 12-04-2011 at 07:53 AM.

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    272
    Rep Power
    4

    Default Re: NEED HELP.. java.lang.ArrayIndexOutOfBoundsException

    I'm confused with this java.lang.ArrayIndexOutOfBoundsException
    Somewhere inside your code you tried to read/write something into your array at the position that is greater/lesser than defined size of array. Often happens with for loops...

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException: 1
    By Aparajita in forum New To Java
    Replies: 3
    Last Post: 08-20-2011, 11:26 PM
  2. Java.lang.ArrayIndexOutOfBoundsException:0>=0
    By maitoti in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2011, 05:03 AM
  3. java.lang.ArrayIndexOutOfBoundsException: 6
    By pyat77 in forum Java Applets
    Replies: 6
    Last Post: 03-06-2010, 01:12 PM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 AM

Posting Permissions

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