Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok so I have an array containing 6 items and I want to make the array still cont

ID: 3624679 • Letter: O

Question

Ok so I have an array containing 6 items and I want to make the array still contain the 6 items and the new 6 spaces be left as null.
My code is below. Kindly correct it and tell me what I did wrong. Thanks in advance


public class BantzTwo
{
//@SuppressWarnings("null")
public static void main (String [] args)
{
double [] cellBills = new double [6];
double [] temp = new double [12];

for (int i = 0; i < cellBills.length; i++)
{
temp[i] = cellBills[i];
}
cellBills = temp;
//temp = null;
for (int i = 0; i < temp.length; i++)
{
System.out.println(temp[i]);
}
}

}

Explanation / Answer

Actually, when you declare a double array, it automatically gets filled with 0s, so in effect, 0.0 signifies a null value.
You can overcome this:
1. Use an object array, each which holds a String representation of the number, so it can have a definite null value.

Here's what I would do:
make a new class:
class valholder
{String val;

valholder(double x)
{
val=Double.toString(x);
}

valholder()
{
val="null";
}

}

Now you create your arrays:
valholder [] cellBills = new valholder [6];
valholder[] temp=new valholder[12];

for (int i = 0; i < cellBills.length; i++)
temp[i] = new valholder(Double.parseDouble(cellBills[i].val));

cellBills = temp;

for (int i = 0; i < temp.length; i++)
{
if(temp[i].val=="null")
System.out.println("null");

else
System.out.println(Double.parseDouble(temp[i].val));
}

2.(the easier way) go through each element after position 6. Whenever you find a 0, initialize it to something like -999. Later, when you find a -999, print "null"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote