Results 1 to 7 of 7

Thread: Array Help

  1. #1
    13patterc is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Array Help

    I have a couple array problems that I can't for the life of me figure out. I've spent countless hours just getting to the point I'm at and even more hours trying to figure out why these two programs still don't work. I am very new to Java so bear with me...

    General guidelines: In order to accomplish this work as quickly as possible, we will not use keyboard or file input. Instead, you may assign the 10 values to your initial arrays in the programs. Using initializer lists to load them with the data types expected for each exercise. Please use methods to do each task. Pass your arrays to the method to complete each task.

    1. Write a program that takes 10 integers as input. The program places the even integers into an array called evenList, the odd integers into an array called oddList, and the negative integers into an array called negativeList.The program displays the contents of the three arrays after all of the integers have been entered.

    Here is my code:
    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class arraysproject1
    {
      private static int i = 0;
      private static int number = 0;
    
      public static void main(String[] args) throws IOException
      {
        int[]  intArray = {13, -2, 10, 7, -15, 20, 51, 87, 22, -90};
        sortEvens(intArray);
        sortOdds(intArray);
        sortNegatives(intArray);
      }
    
      public static void sortEvens(int[] intArray)
        {
          System.out.println("The even numbers are: ");
          int[]  evenList = new int[5];
          int even = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] % 2 == 0){
               intArray[i] = evenList[even];
             }
            }
          System.out.println(evenList[even]);
        }
    
      public static void sortOdds(int[] intArray)
        {
          System.out.println("The odd numbers are: ");
          int[]  oddList = new int[5];
          int odd = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] % 2 != 0){
               intArray[i] = oddList[odd];
              }
             }
          System.out.println(oddList[odd]);
        }
    
      public static void sortNegatives(int[] intArray)
        {
          System.out.println("The negative numbers are: ");
          int[]  negativeList = new int[3];
          int negative = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] < 0){
               intArray[i] = negativeList[negative];
              }
             }
          System.out.println(negativeList[negative]);
        }
    }
    And my output:
    The even numbers are: 0
    The odd numbers are: 0
    The negative numbers are: 0

    2.Write a program that randomly inputs 20 numbers from 1 to 10 into a 2-D array of 5 rows and 4 columns. The program should output the 2-D array, the sum of each row storing numbers in a parallel array and the sum of each column storing number in a parallel array.

    Code:
    Java Code:
    import java.util.*;
    import java.io.*;
    import java.util.Random;
    
    public class arraysproject3
    {
      private static int i = 0;
      private static int j = 0;
    
      public static void main(String[]args)
      {
        int[][] randArray = new int[5][4];
        Random randomizer = new Random();
        for (i = 0; i < randArray.length; i++){
          for (j = 0; j < randArray[i].length; j++){
            randArray[i][j] = randomizer.nextInt(11);
          }
        }
        print(randArray);
        sumRows(randArray);
        sumCols(randArray);
      }
      public static void print(int[][] randArray)
      {
        System.out.println("Elements in 2D Array:");
        for (i = 0; i < randArray.length; i++){
          for (j = 0; j < randArray[i].length; j++){
            System.out.println(randArray[i][j]);
          }
        }
       }
    
      public static void sumRows(int[][] randArray)
      {
        int[] sumOfRows = new int[randArray.length];
        int sumOfR = 0;
        for (int row = 0; row < randArray.length; row++){
          for (int col = 0; col < randArray[row].length; col++){
            sumOfR += randArray[row][col];
          }
        sumOfRows[row] = sumOfR;
        }
        System.out.println("Sum of rows: " + sumOfR);
      }
    
      public static void sumCols(int[][] randArray)
      {
        int[] sumOfCols = new int[randArray[i].length];
        int sumOfC = 0;
        for (int col = 0; col < randArray.length; col++){
          for (int row = 0; row < randArray[col].length; row++){
            sumOfC += randArray[col][row];
          }
        sumOfCols[col] = sumOfC;
        }
        System.out.println("Sum of columns: " + sumOfC);
      }
    
    }
    And output:
    Elements in 2D Array:
    9
    9
    6
    1
    3
    1
    6
    1
    4
    10
    7
    0
    4
    4
    5
    6
    5
    5
    10
    3
    Sum of rows: 99
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at arraysproject3.sumCols(arraysproject3.java:55)
    at arraysproject3.main(arraysproject3.java:28)

    Any guidance on what I'm doing wrong would be great! Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array Help

    You need to inspect the line causing the problem, line 55 of the arraysproject3 class. I am guessing that your line numbers aren't the same as the ones we see here, that there are additional lines at the start of your code not posted here. You should show us the line in question, but I'm guessing that it's this one:

    Java Code:
     int[] sumOfCols = new int[randArray[i].length];
    Your use of i here has a very bad code smell to it. It looks like you used this as an array index, that it incremented all the way to the length of the array, and that use of it now would cause your array to be out of bounds. Never do this. Keep your for loop indices local to the for loop.

  3. #3
    13patterc is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Array Help

    Quote Originally Posted by Fubarable View Post

    Java Code:
     int[] sumOfCols = new int[randArray[i].length];
    Your use of i here has a very bad code smell to it. It looks like you used this as an array index, that it incremented all the way to the length of the array, and that use of it now would cause your array to be out of bounds. Never do this. Keep your for loop indices local to the for loop.
    You are correct, that is the line it's referring to, sorry I didn't specify.
    I understand why it should not be i, but what should it be?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array Help

    For the first part, this is what you are doing inside the sortEven method:
    Java Code:
    if(intArray[i] % 2 == 0) {
       intArray[i] = evenList[even];
    }
    That backwards.
    You are assigning the value of evenList[even] (where even is always zero, and evenList is defaulted to all zeroes) to your original intArray.
    You want it the other way around, and then increment 'even'.
    sortOdd and sortNegative are doing the same thing, though by that point even if they were correct they would be operating on an array of zeroes, after sortEven had finished with it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    13patterc is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Array Help

    Quote Originally Posted by Tolls View Post
    You are assigning the value of evenList[even] (where even is always zero, and evenList is defaulted to all zeroes) to your original intArray.
    You want it the other way around, and then increment 'even'.
    sortOdd and sortNegative are doing the same thing, though by that point even if they were correct they would be operating on an array of zeroes, after sortEven had finished with it.
    I changed it as you said, but it still outputs the same thing!?
    Java Code:
    public static void sortEvens(int[] intArray)
        {
          System.out.println("The even numbers are: ");
          int[]  evenList = new int[10];
          int even = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] % 2 == 0){
               evenList[even] = intArray[i];
               even++;
             }
            }
          System.out.println(evenList[even]);
        }
    
      public static void sortOdds(int[] intArray)
        {
          System.out.println("The odd numbers are: ");
          int[]  oddList = new int[10];
          int odd = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] % 2 != 0){
               oddList[odd] = intArray[i];
               odd++;
               }
             }
          System.out.println(oddList[odd]);
        }
    
      public static void sortNegatives(int[] intArray)
        {
          System.out.println("The negative numbers are: ");
          int[]  negativeList = new int[10];
          int negative = 0;
          for(i = 0; i < 9; i++){
            if(intArray[i] < 0){
               negativeList[negative] = intArray[i];
               negative++;
              }
             }
          System.out.println(negativeList[negative]);
        }
    }
    Did I do something wrong?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Array Help

    You're printing the value of the last entry in your array.
    That is, to take the sortEven method, the value of evenes[even], where even is after the last even entry.

    What you want to do is print out each entry from 0 to even - 1.
    I would write a method that took an array and an int, which would print out each entry in the array with an index < the given int.
    I would make this a method since you are going to use it for all three of your sort methods.
    Java Code:
    for (i = 0; i < maxIndex; i++) {
    print the value in the array at 'i'.
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array Help

    Why all that moving around of the numbers? First you store them in one array, only to be moved to another array a bit later. Why not store them in that other array right away? You have ten numbers; they all can be even or all odd or all negative. So three arrays with ten elements each would be enough. You have to keep track of three counters, telling how many numbers are stored in each array already. While I'm at it, why not make a simple ArrayFiller class that fills a single array (ten elements) number by number? All your main( ... ) method needs to do is create three ArrayFiller objects and hand them each number after having decided which ArrayFiller should store that number. An ArrayFiller should print its contents to top it all off ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 10:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  4. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 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
  •