I am trying to display the values inside an array but there are these 0's that come with it. Using a for loop to count the number of temperature and the temperature's value with it.
This is the code that I used:
import java.util.*;
public class Array1dTemperature {
static Scanner in = new Scanner (System.in);
static Random rng = new Random ();
public static void main(String[] args)
{
System.out.println("This program does a temperature check.");
System.out.println("Input the desired number of temperatures...");
int size = in.nextInt();
int[] temp = new int [size];
for (int i=0; i<temp.length; i )
temp[i] = 1 rng.nextInt(100);
System.out.println("The data file goes as: " Arrays.toString(temp));
Checker(temp);
}
static void Checker (int temp[])
{
int hot[] = new int [10] ; int []pleasant = new int [10]; int []cold = new int [10];
int H = 0; int P = 0; int C = 0;
for (int i=0; i<temp.length;i ) {
if (temp[i]>=85) {
hot[i] = temp[i];
H ;
}
else if (temp[i]>=60&&temp[i]<84) {
pleasant[i] = temp[i];
P ;
}
else if (temp[i]<60) {
cold[i] = temp[i];
C ;
}
}
System.out.println("number of hot: " H ", Recorded temps are: " Arrays.toString(hot) );
System.out.println("number of cold: " C ", Recorded temps are: " Arrays.toString(cold));
System.out.println("number of pleasant: " P ", Recorded temps are: " Arrays.toString(pleasant));
}
}
I tried changing the values of the individual arrays themselves but it becomes out of bounds whenever I try to print the output. I could've used "Arraylist" to update the arrays but this specific exercise problem prohibits the used of such.
CodePudding user response:
So arrays are good for fixed-length data, where you know in advance how much there's going to be. In your case, you don't know in advance how much "real" data will go in your hot/cold/pleasant arrays. Instead of filling them in with the data you do have, in the real world you would use another data structure (like an ArrayList
).
If you absolutely have to use arrays, then you'd want to first loop over the input array once to learn how many of each there are, initialize hot
/etc arrays of the appropriate size, then loop through again to assign them. You would not be able to just assign to the same index as you had from the input array though - you'd instead want to keep track of the next open space in the hot
(or whatever) array, and each time write to and then increment that counter.
CodePudding user response:
You need to use H
, P
, C
as separate indexes for hot
, pleasant
, cold
arrays while populating the arrays and possibly apply Arrays.copyOf
to get rid of zeroes in the tail of those arrays.
Also, there are edge cases to be fixed with setting the temperature type.
static void Checker (int temp[]) {
int[] hot = new int[temp.length];
int[] pleasant = new int[temp.length];
int[] cold = new int[temp.length];
int H = 0; int P = 0; int C = 0;
for (int i = 0; i < temp.length; i ) {
if (temp[i] >= 85) {
hot[H ] = temp[i];
}
else if (temp[i] >= 60) {
pleasant[P ] = temp[i];
}
else {
cold[C ] = temp[i];
}
}
if (H < hot.length) hot = Arrays.copyOf(hot, H);
if (P < pleasant.length) pleasant = Arrays.copyOf(pleasant, P);
if (C < cold.length) cold = Arrays.copyOf(cold, C);
System.out.println("number of hot: " H ", Recorded temps are: " Arrays.toString(hot) );
System.out.println("number of cold: " C ", Recorded temps are: " Arrays.toString(cold));
System.out.println("number of pleasant: " P ", Recorded temps are: " Arrays.toString(pleasant));
}