Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Outofbounds error

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOlved]Outofbounds error

    //Lab 3 Library By Parth Patel
    import java.util.Random;
    import java.util.*;
    import java.io.*;
    import java.util.logging.*;
    public class ParthLibraryLab3{
          int[]array;
          public void userinput(){
              Scanner patel = new Scanner(System.in);
              System.out.println("Please Enter the length of the Random Array: ");
              int size = patel.nextInt();
          }
     
     
     
     
        public void getArrayOfRandomNumbers(int size){
            Random random1 = new Random(System.currentTimeMillis());
            array = new int[size];
            for (int i = 0; i < size; i++){
            array[i] = random1.nextInt(1500);
            //
            }
     
     
           }
          public void displayArrayToScreen(){
                System.out.println("The array contents are: ");
                for(int i = 0; i < array.length; ++i)
     
                System.out.print(array[i]);
     
        }
          public void writeArrayToFile(){
              PrintWriter output = null;
              try{
                  System.out.println("Enter the name of the files to create: ");
                  Scanner PSP = new Scanner (System.in);
                  File file = new File(PSP.nextLine());
                  output = new PrintWriter(file);
                  for(int i = 0; i < array.length; ++i){
                      output.print(array[i] + ",");
                  }
     
              }catch(IOException ex){
                  Logger.getLogger(ParthLibraryLab3.class.getName()).log(Level.SEVERE, null, ex);
     
              }finally{
                  output.close();
              }
     
          }
        public void selectionSort(int[] array, int startingIndex){
            if(startingIndex >= array.length +1){
                return;
            }
            int minimumIndex = startingIndex;
            for(int index = startingIndex -1; index < array.length; index++){
                if(array[index] < array[minimumIndex]){
                    minimumIndex = index;
                }
                startingIndex = indexOfSmallest();
            }
            int temporary = array[startingIndex];
            array[startingIndex] = array[minimumIndex];
            array[minimumIndex] = temporary;
            selectionSort(array,startingIndex +
                    +1);
        }
            public void SelectionSort(){
              int LargestNumber = 0;
              int SmallestNumber = 0;
              int IndexNumber = 0;
              for(int i = 1; i < array.length; ++i)
                  if(array[i] > array[i-1]){
                      LargestNumber = array[i];
                      IndexNumber = i;
                  }
              SmallestNumber = indexOfSmallest();
          }
     
          public int indexOfSmallest(){
              int result = 2000;
              for(int i = 1;i < array.length; ++i)
                  if(array[i] < array[i-1])
                      result = i;
              return result;
          }
     
          }
    Main
    //Lab 3 By Parth Patel Main
     
    public class ClientParth {
        public static void main(String[] patel){
     
     
      ParthLibraryLab3 Snowflake = new ParthLibraryLab3();
     
      Snowflake.userinput();
      Snowflake.getArrayOfRandomNumbers(3);
      Snowflake.writeArrayToFile();
      Snowflake.displayArrayToScreen();
      Snowflake.indexOfSmallest();
      Snowflake.SelectionSort();
      Snowflake.selectionSort(Snowflake.array, 0);
     
     
     
     
     
     
     
     
     
        }
     
     
    }
    the error

    run:
    Please Enter the length of the Random Array:
    4
    Enter the name of the files to create:
    5
    The array contents are:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at ParthLibraryLab3.selectionSort(ParthLibraryLab3.ja va:59)
    at ClientParth.main(ClientParth.java:15)
    28884980Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)

    Help me, please!
    Last edited by tendulkarfan4life; February 14th, 2011 at 04:56 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Outofbounds error

    First,

    In main function you call Snowflake.selectionSort(Snowflake.array, 0). If startingIntex has zero value, then index starts with -1 and the array[-1] is called.

     
    for (int index = startingIndex - 1; index < array.length; index++) {
     
    				if (array[index] < array[minimumIndex]) {
    					minimumIndex = index;
     
    				}
    				startingIndex = indexOfSmallest();
    			}

    Second,

    is that correct?

     
    if(startingIndex >= array.length + 1){
    				return;
    			}

    I would think so:

     
    if(startingIndex >= array.length){
    				return;
    			}

    because the last index of array is (size - 1)

    helped?

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Outofbounds error

    And my main looks like this:
    public class ClientParth {
    public static void main(String[] patel){


    ParthLibraryLab3 Snowflake = new ParthLibraryLab3();

    Snowflake.userinput();
    Snowflake.getArrayOfRandomNumbers(3);
    Snowflake.writeArrayToFile();
    Snowflake.displayArrayToScreen();
    Snowflake.indexOfSmallest();
    Snowflake.SelectionSort();
    Snowflake.selectionSort(Snowflake.array, -1);

    Hmmm i get this out put with the following mistakes corrected.

    run:
    Please Enter the length of the Random Array:
    5
    Enter the name of the files to create:
    parth
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
    The array contents are:
    at ParthLibraryLab3.selectionSort(ParthLibraryLab3.ja va:59)
    at ClientParth.main(ClientParth.java:15)
    8781252382Java Result: 1

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Outofbounds error

    Your code above says you're still starting at less than 0.

    You can't do that.


    if(startingIndex >= array.length +1){
    return;

    If that is turned to

    if (startingIndex >= array.length || startingIndex < 0)
    {
    return;

    Your problem should go away...though if the problem is not from that directly...it'll return and not sort (as you told it to.)

    Ahhhh...got it I think

    for(int index = startingIndex -1; index < array.length; index++){

    You shouldn't start at less than 0.

    However, index should never be less than 0, which it will be even if starting index is 0. It'll say IndexOutOfBounds -1.

    As you're starting with -1, it's saying IndexOutOfBounds -2.

    Try changing that to

    for (int index = startingIndex(perhaps); index < array.length; index++)
    Last edited by javapenguin; February 13th, 2011 at 10:59 PM. Reason: Found the problem! :)

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Outofbounds error

    thanks you, it worked!