Why does my attempt to print the index of my array ALWAYS return 0.00?

42 views Asked by At

This is the section of code I am talking about. I have ensured all braces are accurate. At the end I have two other print statements to prove that the correct values ARE in the array, but when I use System.out.printf to try and print the entire index, it just returns 0.00. I have printed indexes with System.out.print(array[i]); before, and it worked fine. Other types of print commands besides the one with the format command also return 0.00. I'm learning Java and this is making me pull my hair out. Thank you.

Scanner scnr = new Scanner(System.in);
int numVals;
int i;
double maxDouble;
double[] myVals = new double[20];
numVals = scnr.nextInt();
for (i=0; i < numVals; ++i) {
    myVals[i] = scnr.nextDouble();
}
  
maxDouble = myVals[0];
for (i=0; i<numVals; ++i) {
      if (maxDouble < myVals[i]) {
          maxDouble = myVals[i];
      }
}
System.out.println(maxDouble); // THIS CORRECTLY RETURNS MAXDOUBLE
System.out.println(myVals[2]); // THIS CORRECTLY RETURNS DATA FROM [2] 
System.out.printf("%.2f", myVals[i]); //THIS ALWAYS RESULTS IN 0.00
System.out.println(myVals[i]); //THIS ALSO RETURNS 0

Multiple print commands, proving the values are in the array and initialized correctly, rewriting and formatting print command.

0

There are 0 answers