# how to read an integer of DOUBLE datatype with type casting

Printable View

• 12-14-2010, 08:18 PM
amrmb09
how to read an integer of DOUBLE datatype with type casting
please, tell me how to read an integer of DOUBLE data type using
1-readInt() with tpe casting.
2-any other approach

Code:

```ublic static void main(String [] args) {         Scanner read = new Scanner(System.in);         double radius;   Circle []c1 =new Circle[5];       for (int i=0;i<c1.length;i++)     {             System.out.println("radius("+(i+1)+")= ");             radius=read.nextInt();             c1[i] = double new Circle2(radius);             System.out.print("obj("+(i+1)+").Area= "+c1[i].area());             System.out.print("obj("+(i+1)+").Area= "+c1[i].circum());             System.out.print("obj("+(i+1)+").Area= "+c1[i].diam());     }```
• 12-14-2010, 08:51 PM
javaman1
You can just use "nextDouble()" instead of "nextInt()".
• 12-14-2010, 09:01 PM
JosAH
Quote:

Originally Posted by amrmb09
please, tell me how to read an integer of DOUBLE data type using
1-readInt() with tpe casting.
2-any other approach

A (primitive) data type is either int or double; it can't be an int of double type or vice versa; casting is easy though:

Code:

```double d= ...; int i= (int)d;```
It doesn't matter how d got its value, you don't even need an explicit d variable:

Code:

`int i= scanner.nextDouble();`
In this code fragment a scanner reads a double value which is cast to an int and assigned to an int variable i. Casting the other way around (ints to doubles) is even easier, i.e. you don't need the explicit cast operation.

kind regards,

Jos