# How to make user input be of the type double?

• 08-06-2012, 12:32 AM
abv
How to make user input be of the type double?
this line

arr[i] = sc.nextDouble();

gives this warning

- Type mismatch: cannot convert from double to int
• 08-06-2012, 12:54 AM
Fubarable
Re: How to make user input be of the type double?
How do you declare your array, arr? is it double[] arr or int [] arr?
• 08-06-2012, 01:09 AM
abv
Re: How to make user input be of the type double?
it's double[]
• 08-06-2012, 01:21 AM
Fubarable
Re: How to make user input be of the type double?
Quote:

Originally Posted by abv
it's double[]

Hm,... what about i? Is that double or int? To make it easier on us all, could you post your code?
• 08-06-2012, 01:23 AM
abv
Re: How to make user input be of the type double?
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: ");
int n = sc.nextInt();
double[] arr = new double[n];
double min = 0;
for(double i = 0; i <n; i++){
System.out.println("Enter element " + (i + 1));
arr[i] = sc.nextDouble();
if(i%3 == 0 && i <= min){
min = i;
}
}
if(min != 0){
System.out.println("The smallest number divisible by 3 is" + min);
}
else{
System.out.println("No number is divisible by 3");
}
}
so i had to be int
• 08-06-2012, 01:36 AM
DarrylBurke
Re: How to make user input be of the type double?
• 08-06-2012, 01:38 AM
DarrylBurke
Re: How to make user input be of the type double?
Why exactly did you declare your for-loop index as of type double?

db
• 08-06-2012, 01:57 AM
Fubarable
Re: How to make user input be of the type double?
Quote:

Originally Posted by DarrylBurke
Why exactly did you declare your for-loop index as of type double?

Yikes! so i is in fact a double when it should be an int. You should be able to fix this now.
• 08-06-2012, 06:08 AM
Junky
Re: How to make user input be of the type double?
Quote:

Originally Posted by DarrylBurke
Why exactly did you declare your for-loop index as of type double?

db

Maybe in the future they plan on accessing the 1 and halfth element of the array.
• 08-06-2012, 09:24 AM
DarrylBurke
Re: How to make user input be of the type double?
Quote:

Originally Posted by Junky
Maybe in the future they plan on accessing the 1 and halfth element of the array.

Fuzzy logic?

db