When analysing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This adjustment can be done by normalizing to values between 0 and 1, or throwing away outliers.
For this program, adjust the values by dividing all values by the largest value. The input begins with an integer indicating the number of floating-point values that follow. Assume that the list will always contain fewer than 20 floating-point values.
Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
System.out.printf("%.2f", yourValue);Ex: If the input is:
5 30.0 50.0 10.0 100.0 65.0
the output is:
0.30 0.50 0.10 1.00 0.65
The 5 indicates that there are five floating-point values in the list, namely 30.0, 50.0, 10.0, 100.0, and 65.0. 100.0 is the largest value in the list, so each value is divided by 100.0.
For coding simplicity, follow every output value by a space, including the last one.
This is my code so far:
import java.util.Scanner;
public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); double numElements; numElements = scnr.nextDouble(); double[] userList = new double[numElements]; int i; double maxValue; for (i = 0; i < userList.length; ++i) { userList[i] = scnr.nextDouble(); } maxValue = userList[i]; for (i = 0; i < userList.length; ++i) { if (userList[i] > maxValue) { maxValue = userList[i]; } } for (i = 0; i < userList.length; ++i) { userList[i] = userList[i] / maxValue; System.out.print(userList[i] + " "); System.out.printf("%.2f", userList[i]); } }
}I keep getting this output.
LabProgram.java:8: error: incompatible types: possible lossy conversion from double to int
double [] userList = new double [numElements]; ^
1 errorI think my variable is messed up. I read through my book and could not find help. Can someone please help me on here. Thank you so much! This has been very stressful for me.
12 Answers
The specific error message is because the index and size of an element must be int. So declare and assign at once: int numElements = scnr.nextInt();
Better way of programming things:
- skip manual input (aka Scanner and consorts). Makes you crazy and testing a 100'000'000 times slower
- you can integrate the interactive part later, once the method is done. You already know how, your code already shows.
- use an explicit method to do your work. Don't throw everything into the main method. This way you can run multiple examples/tests on the method, and you have a better implementation for later.
- check for invalid input INSIDE the method that you implement. Once you can rely in such a method, you can keep on using it later on.
- you could even move the example numbers to its own test method, so you can run multiple test methods. You will learn about Unit Testing later on.
Example code:
public class LabProgram { public static void main(final String[] args) { final double[] initialValues = new double[] { 30.0, 50.0, 10.0, 100.0, 65.0 }; final double[] adjustedValues = normalizeValuesByHighest(initialValues); System.out.println("Adjusted values:"); for (final double d : adjustedValues) { System.out.printf("%.2f ", Double.valueOf(d)); } // expected otuput is 0.30 0.50 0.10 1.00 0.65 System.out.println(); System.out.println("All done."); } static public double[] normalizeValuesByHighest(final double[] pInitialValues) { if (pInitialValues == null) throw new IllegalArgumentException("Invalid double[] given!"); if (pInitialValues.length < 1) throw new IllegalArgumentException("double[] given contains no elements!"); // detect valid max value double tempMaxValue = -Double.MAX_VALUE; boolean hasValues = false; for (final double d : pInitialValues) { if (Double.isNaN(d)) continue; tempMaxValue = Math.max(tempMaxValue, d); hasValues = true; } if (!hasValues) throw new IllegalArgumentException("double[] given contains no valid elements, only NaNs!"); // create return array final double maxValue = tempMaxValue; // final from here on final double[] ret = new double[pInitialValues.length]; for (int i = 0; i < pInitialValues.length; i++) { ret[i] = pInitialValues[i] / maxValue; // NaN will stay NaN } return ret; }
}Output:
Adjusted values:
0,30 0,50 0,10 1,00 0,65
All done. 2