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 3 of 3

Thread: IndexOutOfBounds.. Error

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default IndexOutOfBounds.. Error

    Hey guys, I just joined the forums and hope to learn a lot about programming here.

    So, I have an assignment to make a code calculating the mode of a text file with 999 integers. I know there are probably more efficient ways to solve this problem, but I want to stick with my code and just fix it. I'm trying to make one ArrayList with the occurrences(frequencies) of each of the 999 integers in the text file, and get the mode that way.

    I keep on receiving this error
    "IndexOutOfBoundsException: Index: 1, Size: 1)"
    when I try to run my code, and the highlighted error is on my "numbers.remove..." and on "backUp.remove..." in my public intCalcFinalMode() method. I know what the error means, but I've tried everything and I cannot find a way to fix it. Can someone please point out my error? Thanks.



    import java.util.ArrayList;
    import java.lang.Math;
     
     
    public class Statistics
    {
    private ArrayList<Integer> numbers = new ArrayList<Integer>();
    private ArrayList<Integer> occurrences =new ArrayList<Integer>();
    private ArrayList<Integer> finalMode = new ArrayList<Integer>();
    private ArrayList<Integer> backUp = new ArrayList<Integer>();
     
    public Statistics()
    {
    } //end default constructor
     
    public Statistics(ArrayList<Integer> inputNumbers)
    {
    numbers = inputNumbers;
    } //end non-default constructor
    public void calcOccurrences()
    {
     
    for(int currentNumber : numbers)
    {
    int counter = 0;
    int numberOfOccurrences = 0;
    while (counter < numbers.size())
    {
    if (currentNumber == numbers.get(counter))
    {
    numberOfOccurrences++;
    counter++; 
    } else {
    counter++;
    } //end if
     
     
     
     
    } //end while statement
    occurrences.add(numberOfOccurrences); 
    backUp.add(numberOfOccurrences);
     
    } //end extended for loop
    } //end method
     
    public int calcFinalMode()
    {
     
     
    for(int currentOccurrence : occurrences)
    {
    int facto=0;
    int counter = 0;
    while (counter < occurrences.size())
    {
    if (currentOccurrence >= occurrences.get(counter))
    {
    counter++;
     
     
    } else {
     
    [B]numbers.remove(occurrences.indexOf(currentOccurrence));
    backUp.remove(occurrences.indexOf(currentOccurrence));[/B]
    } //end LONG if
     
    } // end while loop
    facto = numbers.get(backUp.indexOf(currentOccurrence)); 
    finalMode.add(facto);
     
    } //end extended for loop
     
    return finalMode.get(0);
     
    } //end method
     
    public int calcOtherMode()
    {
    int otherMode = 0;
    finalMode.remove(0);
    if (!finalMode.isEmpty())
    {
    otherMode = finalMode.get(0);
    } //end if statment
    return otherMode;
    } //end method
     
     
    } //end class
     
    DRIVER --------------------------------
     
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class StatisticsDriver
    {
    public static void main(String args[])
    {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
     
    Scanner in;
    try {
    in = new Scanner(new File("numbers.txt"));
    while(in.hasNextInt())
    {
    numbers.add(in.nextInt());
    } //end while loop
     
    } catch(IOException i) {
    System.out.println("Error: " + i.getMessage());
    } //end file-read
     
    Statistics myStatistics = new Statistics(numbers); 
    myStatistics.calcOccurrences();
    System.out.println("Mode: " + myStatistics.calcFinalMode() + ", " + myStatistics.calcOtherMode());
     
    }// end main
     
    } //end class
    Last edited by Arius; January 17th, 2010 at 02:32 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: IndexOutOfBounds.. Error

    Hello Arius.

    When I compile this code I don't get any errors? I ran it against my own version of numbers.txt and get this output:

    Mode: 1, 1

    What would you expect the output to be?

    Could you please attach your version of numbers.txt. I need to try to replicate the error...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: IndexOutOfBounds.. Error

    [ATTACH]numbers.txt[/ATTACH]

    I think I just uploaded it. I know for a fact that the two modes for this text file are 10 and 48. When you ran your version of numbers, it gave you the correct mode? That is odd.

    In researching the solution for this problem, I actually found that you cannot remove inside of a for loop, so I revised me code as well. If you can correct the problem on either one and make the modes (10, 48) actually appear, I will be eternally grateful, since I've been working on this for 5 days straight with no success. However, I know some methods such as hash and etc are more effective, my assignment requires that I stick with only array lists and no more importations... Thank you.
    import java.util.ArrayList;
    import java.lang.Math;
     
     
    public class Statistics
    {
        private ArrayList<Integer> numbers = new ArrayList<Integer>();
        private ArrayList<Integer> occurrences =new ArrayList<Integer>();
        private ArrayList<Integer> finalMode = new ArrayList<Integer>();
        private ArrayList<Integer> backUp = new ArrayList<Integer>();
     
        public Statistics()
        {
        } //end default constructor
     
        public Statistics(ArrayList<Integer> inputNumbers)
        {
            numbers = inputNumbers;
        } //end non-default constructor
     
        public double calcAverage()
        {
            int total = 0;
            for(int currentNumber : numbers) //don't need to specify size cuz goes for duration of arraylist
            {
              total += currentNumber; 
            } //end extended for loop
     
            return (total/numbers.size());
     
        } //end method
     
        public double calcStandardDeviation()
        {
            double total = 0;
            for(int currentNumber : numbers)
            {
                double partOfSum = Math.pow((currentNumber - calcAverage()), 2);
                total += partOfSum;
            } //end extended for
     
            return Math.pow((total/(numbers.size() - 1)), 0.5);
     
        } //end method
     
        public void calcOccurrences()
        {
     
            for(int currentNumber : numbers)
            {
                int counter = 0;
                int numberOfOccurrences = 0;
                while (counter < numbers.size())
                {
                    if (currentNumber == numbers.get(counter))
                    {
                        numberOfOccurrences++;
                        counter++;   
                    } else {
                        counter++;
                    } //end if
     
     
     
     
                } //end while statement
                occurrences.add(numberOfOccurrences); 
                backUp.add(numberOfOccurrences);
     
             } //end extended for loop
        } //end method
     
       public void calcFinalMode()
       {
     
     
           for(int currentOccurrence : occurrences)
           {
     
               int counter = 0; 
               while (counter < occurrences.size() && counter > 0)
               {
                    if (currentOccurrence >= occurrences.get(counter))
                    {
                        counter++;
     
                    } else {
                        numbers.set((occurrences.indexOf(currentOccurrence)), 0);
                        backUp.set((occurrences.indexOf(currentOccurrence)), 0);
                        counter = 1000;
     
                    } //end LONG if
     
               } // end while loop
             if (!(currentOccurrence == 0))
             {
                int facto = numbers.get(backUp.indexOf(currentOccurrence));   
                finalMode.add(facto);
            } // end if
            int facto=0;
     
     
            } //end extended for loop
     
        } //end method
     
        public int calcDefMode()
        {
            int counter = 0;
            int facto = 0;
            while (counter <  finalMode.size())
            {
                if (finalMode.get(counter) > 0)
                {
                    facto = finalMode.get(counter);
                    counter = 1000;
                } //end if statment
            } //end while
            return facto;
        } //end method
     
     
     
     
    } //end class

    DRIVER --------------------------------------------------------
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class StatisticsDriver
    {
        public static void main(String args[])
        {
            ArrayList<Integer> numbers = new ArrayList<Integer>();
     
            Scanner in;
            try {
            in = new Scanner(new File("numbers.txt"));
            while(in.hasNextInt())
            {
               numbers.add(in.nextInt());
            } //end while loop
     
            } catch(IOException i) {
               System.out.println("Error: " + i.getMessage());
            } //end file-read
     
            Statistics myStatistics = new Statistics(numbers); //why can't put "ArrayList inputNumbers"????
            System.out.println("Average: " + myStatistics.calcAverage());
            System.out.println("Standard Deviation: " + myStatistics.calcStandardDeviation());
            myStatistics.calcOccurrences();
            myStatistics.calcFinalMode();
            //System.out.println("Mode: " + myStatistics.calcFinalMode()  +  ", " + myStatistics.calcOtherMode());
            System.out.println("Mode: " + myStatistics.calcDefMode());
     
         }// end main
     
    } //end class
    Attached Files Attached Files
    Last edited by Arius; January 17th, 2010 at 04:17 PM.