Results 1 to 3 of 3
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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 07:22 PM.

  2. #2
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    You can just use "nextDouble()" instead of "nextInt()".

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by amrmb09 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why is there (Integer) casting?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 11-15-2010, 10:43 AM
  2. Replies: 6
    Last Post: 11-02-2010, 04:14 PM
  3. Type Casting
    By Shaheen Mohamed in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 07:56 PM
  4. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 07:07 PM
  5. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM

Posting Permissions

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