Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Junky

Thread: How to make user input be of the type double?

  1. #1
    abv
    abv is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  3. #3
    abv
    abv is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to make user input be of the type double?

    it's double[]

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to make user input be of the type double?

    Quote Originally Posted by abv View Post
    it's double[]
    Hm,... what about i? Is that double or int? To make it easier on us all, could you post your code?

  5. #5
    abv
    abv is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: How to make user input be of the type double?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to make user input be of the type double?

    Quote Originally Posted by DarrylBurke View Post
    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.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: How to make user input be of the type double?

    Quote Originally Posted by DarrylBurke View Post
    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.
    DarrylBurke likes this.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: How to make user input be of the type double?

    Quote Originally Posted by Junky View Post
    Maybe in the future they plan on accessing the 1 and halfth element of the array.
    Fuzzy logic?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 01-02-2012, 11:16 PM
  2. Replies: 2
    Last Post: 09-27-2011, 06:46 PM
  3. double type format
    By eleda in forum New To Java
    Replies: 4
    Last Post: 09-17-2010, 07:23 AM
  4. is Math.pow only used with type double?
    By AprilFlowers in forum New To Java
    Replies: 9
    Last Post: 10-23-2009, 04:38 AM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

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