Reputation: 13
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_VALS = 5;
int [] personsWeight = new int[NUM_VALS];
int i = 0;
for (i = 0; i < NUM_VALS; ++i) {
System.out.print("Enter weight " + (i + 1) + ": ");
System.out.println();
personsWeight[i] = scnr.nextInt();
}
double sumVal = 0.0;
for (i = 0; i < NUM_VALS; ++i) {
sumVal = sumVal + personsWeight[i];
}
double avgVal = 0.0;
for (i = 0; i < NUM_VALS; ++i) {
avgVal = sumVal / NUM_VALS;
}
double maxVal = 0.0;
for (i = 0; i < NUM_VALS; ++i) {
maxVal = personsWeight[0];
if (maxVal < personsWeight[i]) {
maxVal = personsWeight[i];
}
}
System.out.println();
System.out.println(personsWeight[1] + " " + personsWeight[2] + " "
+ personsWeight[3] + " " + personsWeight[4] + " " + personsWeight[5]);
//I know that this can be cleaner but this is where the
//problem occurs with a InputMismatchException.
//I know it's caused by the double being introduced
//but don't know how to fix it.
System.out.println("Total weight: " + sumVal);
System.out.println("Average weight: " + avgVal);
System.out.println("Max weight: " + maxVal);
return;
}
}
The inputs are as follows: 236 , 89.5 , 142 , 166.3 , 93 .
I want to handle the output numbers just as they are input. Double or Int.
Is there anyway I can make the Array accept both types of numbers in the scanner or would I have to resort another way that it works?
Upvotes: 1
Views: 2585
Reputation: 31689
When you have a list of numbers that all represent the same kind of value (such as weight, money amount, test score, etc.), pick one type that's appropriate for all the values that you'd want to handle. For weight, assuming you want to handle non-integers, choose a floating-point type (i.e. double
). For money, you might choose BigDecimal
; for test scores, you might choose int
unless you expect someone might get half a point deducted for something, then you'd choose double
. The point is, there is almost never a reason to keep different numeric types for the same kind of value.
There is almost never a reason to care about what format the input is in--i.e. do you really care whether a weight in the input has a decimal point or not? No, you only care about the value itself. This confuses some newer programmers--they equate the value of the number with its representation as a string. But almost always, only the value matters. There's no difference between the numbers 236, 236.0, 236.00000, 2.36e2, and 0xec--the value is the same, and the representation doesn't matter. [Yes, I know that if you use these different forms in your Java program, some will be treated as int
and some as double
, which could make a difference. But the numbers here are in the input file, not in the code.]
So just make the weight a double
and don't worry about integers.
double[] personsWeight = new double[NUM_VALS];
int i = 0;
for (i = 0; i < NUM_VALS; ++i) {
System.out.print("Enter weight " + (i + 1) + ": ");
System.out.println();
personsWeight[i] = scnr.nextDouble();
}
Upvotes: 0
Reputation: 3
An array can define only one type, you can define double
there, like double [] personsWeight = new double[NUM_VALS];
and when you input an int
type, like 236
, it will be accepted as a double
into Array, output as double
too.
Upvotes: 1
Reputation: 31841
You can determine initially if the inputted number is Double or Integer using hasNextInt()
or hasNextDouble()
methods of Scanner
class.
if (scnr.hasNextInt()) {
int n = scnr.nextInt();
//your code to handle `integer` case
}
else if(scnr.hasNextDouble()) {
double d = scnr.nextDouble();
//your code to handle `double` case
}
FYI, you can also keep everything as double
. It will handle both int
s and double
s and will execute without any error.
Upvotes: 1
Reputation: 41
The simplest way I can think to solve this is to take in String objects from the Scanner
and then convert them to their actual type (you can detect that it's a double if the String contains a period) when you need to use their value for any calculation.
Upvotes: 0