Jump to content

Java Array adding zeros in?


Recommended Posts

Hey, guys. I'm working on Java program that is suppose to let the user put in 10 numbers through input boxes, calculate the average of the number summed up numbers then display the array, the numbers ABOVE the average and the numbers BELOW the average. The code below gets me very close, but errors out when I put a variable number on the array size (int[] overAverage = new int[10]). This is a screen shot of the error:[removed]I've tried all that I can think of, but this is by far the closest I've gotten to making it work even closely right. If I give my array size a fixed size of 10, it won't error out, however it gives me a undesired result, it adds in magical zeros in what should be nothing:[removed]Can sometone help me understand this problem? I'm new to Java but I can do basic code in it.

package ten_number_average;import javax.swing.JOptionPane;public class Main {  public static void main(String[] args) {    int addedNumber = 0;    int average = 0;    int k_count = 0;    int zero_count = 0;    int[] arrayNumber = new int[10];    for (int i = 0; i < 10; i++) {      int one_plus = i + 1;      String intNumber = JOptionPane.showInputDialog(null, "Enter the " +one_plus+ " of 10 number:", one_plus*5);      arrayNumber[i] = Integer.parseInt(intNumber);      int number = Integer.parseInt(intNumber);      addedNumber = addedNumber + number;    }    average = addedNumber / 10;    String arrayOutput = "The array: ";    for (int k = 0; k < 10; k++) {      arrayOutput += arrayNumber[k] + " ";      if (arrayNumber[k] > average) {        k_count++;      }      if (arrayNumber[k] <= average) {        zero_count++;      }    }//    final int averageCount = k_count;//    final int notaverageCount = zero_count;    int[] overAverage = new int[10]; // k_count || 10    int[] underAverage = new int[10]; // zero_count || 10    arrayOutput += "\nOver the average (" +average+ "): " ;    for (int n = 0; n < 10; n++) {      if (arrayNumber[n] > average) {        overAverage[n] = arrayNumber[n];      }      else {        underAverage[n] = arrayNumber[n];      }    }    for (int y = 0; y < overAverage.length; y++) {      arrayOutput += overAverage[y] + " ";    }    arrayOutput += "\nUnder the average: ";    for (int u = 0; u < underAverage.length; u++) {      arrayOutput += underAverage[u] + " ";    }    JOptionPane.showMessageDialog(null, arrayOutput);  }}

Edited by RahXephon
Link to post
Share on other sites

This is one disturbing (at least at first) aspect to JAVA. Arrays, as well as other primitive types are... what was the word... immutable I think. It means that they can't be changed once they are created. In the case of arrays, their size can't be changed.You cau use java.util.ArrayList to get a resizable array. Be sure to check its documentation, as adjustings are done by methods, rather than with primitive operations as with PHP.

Link to post
Share on other sites

I wasn't sure at first, but got the same conclusion that the arrays can't be changed. So, I made my arrays AFTER I know how big it is going to be. I use:

for (int k = 0; k < 10; k++) {  if (arrayNumber[k] > average) {	k_count++;  }}

k_count tells me how many times the numbers inside arrayNumber is above the average. I use this as the size of my new array (int[] overAverage = new int[k_count]), since I can't change the array size, I make my array after I know how many of what I'm putting inside of it. So in essence, I don't need to resize my array since I already have it the size that I want. I'll keep java.util.ArrayList in mind though.

Link to post
Share on other sites

Yes, arrays cannot be changed, because when you initialize an array the program reserves an amount of memory on the RAM based on the length of the array you specify. However, if you were to assign to a subscript of the array higher than the length you specified there wouldn't be any (reserved) memory for it to be stored to. So you can't.In C and C++ it is possible to write past the end of an array but doing so will cause whatever memory addresses lie beyond the array to be overwritten, causing anything from program variables to change to your computer crashing.

Link to post
Share on other sites
However, if you were to assign to a subscript of the array higher than the length you specified there wouldn't be any (reserved) memory for it to be stored to. So you can't.
Hence the "ArrayIndexOutOfBoundsException" that is thrown at runtime. You were attempting to access an element of the array using an index that was outside of the defined bounds of the array.
Link to post
Share on other sites

I got it guys. After some hard thinking, I managed to make it work exactly the way I wanted it. Thanks for all help in this matter :) The code below has the working edits in red.SS: http://i7.photobucket.com/albums/y268/HYPN...ing_program.jpg

package ten_number_average;import javax.swing.JOptionPane;public class Main {  public static void main(String[] args) {    int addedNumber = 0;    int average = 0;    int k_count = 0;    int zero_count = 0;    [color="#FF0000"]int pos1 = 0;    int pos2 = 0;[/color]    int[] arrayNumber = new int[10];    for (int i = 0; i < 10; i++) {      int one_plus = i + 1;      String intNumber = JOptionPane.showInputDialog(null, "Enter the " +one_plus+ " of 10 number:", one_plus*5);      arrayNumber[i] = Integer.parseInt(intNumber);      int number = Integer.parseInt(intNumber);      addedNumber = addedNumber + number;    }    average = addedNumber / 10;    String arrayOutput = "The array: ";    for (int k = 0; k < 10; k++) {      arrayOutput += arrayNumber[k] + " ";      if (arrayNumber[k] > average) {        k_count++;      }      if (arrayNumber[k] <= average) {        zero_count++;      }    }    int[] overAverage = new int[[color="#FF0000"]k_count[/color]];    int[] underAverage = new int[[color="#FF0000"]zero_count[/color]];    arrayOutput += "\nOver the average (" +average+ "): " ;    for (int n = 0; n < 10; n++) {      if (arrayNumber[n] > average) {        [color="#FF0000"]if (pos1 == k_count) {          continue;        }[/color]        overAverage[[color="#FF0000"]pos1[/color]] = arrayNumber[n];        [color="#FF0000"]pos1++;[/color]      }      else {        [color="#FF0000"]if (pos2 == zero_count) {          continue;        }[/color]        underAverage[[color="#FF0000"]pos2[/color]] = arrayNumber[n];        [color="#FF0000"]pos2++;[/color]      }    }    for (int y = 0; y < overAverage.length; y++) {      arrayOutput += overAverage[y] + " ";    }    arrayOutput += "\nUnder the average: ";    for (int u = 0; u < underAverage.length; u++) {      arrayOutput += underAverage[u] + " ";    }    JOptionPane.showMessageDialog(null, arrayOutput);  }}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...