Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Need help converting from int to double

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help converting from int to double

    I know you have to cast in double but I don't know how to put it I tried everything... so i think, the program sorts numbers from an arraylist and then checks to see if your right in the int[] sorted values

     
    public void testSortDecimal() {
     
    		int[] unsorted = {1.1, .9, 3.2};
    		int[] sorted   =  { .9, 1.1, 3.2};
    		IterativeSorter is = new IterativeSorter(unsorted);
    		int[] a = is.sort();
    		assertArrayEquals(sorted, a);
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Need help converting from int to double

    Why are you trying to initialize an int array with double values?
    Why not define the array as double?

    Do you know how to use casting? Put the cast to type in ()s
    For example double=2.3 to int=2 by using (int):
          System.out.println("cast to int: "+(int)2.3);  // cast to int: 2
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help converting from int to double

    I would but Im not allowed to change the class for the project so i can't change the array, is there a way I can cast double into the method I tried it this way but got errors..

     
    int[] unsorted = {(double)1.1, (double).9,(double) 3.2};

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Need help converting from int to double

    The definition of unsorted is an int array. To the right of the = should be a list of int values. The casting being done is redundant. 1.1 is a double.
    Changing the (double) to (int) should work because it will change what is on the righthand side of the = to a list of int values suitable for initializing an int array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help converting from int to double

    If I have the int array and want to have decimals be seen and come back as a decimal how can I write a method that will notice decimals?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Need help converting from int to double

    If I have the int array and want to have decimals
    You need to decide which you want to have: int or decimals

    how can I write a method that will notice decimals?
    Please explain what "notice decimals" means.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help converting from int to double

    For example, this code takes the numbers that are given, -12,-5,-20,-67 then I put them in order from -67 to -5, then when I test this the program goes through and checks to make sure the sorted values match the sorted values that the program comes up with. If both the computer and my answer match up then i get a green check.

    @Test
    	public void testSortAllNegative() {
    		int[] unsorted = {-12, -5, -20, -67};
    		int[] sorted   = {-67, -20, -12, -5};
    		IterativeSorter is = new IterativeSorter(unsorted);
    		int[] a = is.sort();
    		assertArrayEquals(sorted, a);
    	}


    I need to make a new method that follows this one but I want it to work with decimal numbers, I know that in order to do that I need to change the data type but the arraylist is type int but I can't edit the arraylist class. If

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Need help converting from int to double

    make a new method that follows this one but I want it to work with decimal
    Can you post the code for the method you are trying to make and explain the problems you are having doing it?

    the arraylist is type int but I can't edit the arraylist class.
    Where is the arraylist defined? The ArrayList class can be defined to hold Double objects: ArrayList<Double>
    or arrays of doubles: ArrayList<double[]>
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 10:00 PM
  2. [SOLVED] double to int problem...
    By proxyspam in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 1st, 2012, 03:24 PM
  3. Converting a String to an Int? Is it possible?
    By Gravity Games in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2012, 11:21 PM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. converting int to char
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 11:59 PM