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

1. Member
Join Date
Nov 2010
Posts
75
Rep Power
0

## 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

Java 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());
}```
Last edited by amrmb09; 12-14-2010 at 08:22 PM.

2. Member
Join Date
Sep 2010
Posts
56
Rep Power
0
You can just use "nextDouble()" instead of "nextInt()".

3. 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:

Java 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:

Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•