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 12 of 12

Thread: changing datatype failure

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default changing datatype failure

    public class Ex21{
    	public static void main(String args[]){
    		int num1, num2, num3, total1, total2;
    		num1 = Keyboard.readInt();
    		num2 = Keyboard.readInt();
    		num3 = Keyboard.readInt();
    		total1 = num1+num2+num3;
    		total2 = (double)total1/3;
     
     
    	}
    }
    the last row is the error and my exercise from the book says i have to do it this way instead of initializing the varible a double
    anyone know how to solve this?
    thank you


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: changing datatype failure

    This looks familiar!
    http://www.javaprogrammingforums.com...-question.html

    Are you two taking the same class?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: changing datatype failure

    i think so yeah im learning java too but his question seemed a little different
    do you know how to solve it?

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: changing datatype failure

    Can you copy and paste the error message you got?

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: changing datatype failure

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from double to int

    at Ex21.main(Ex21.java:8)

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: changing datatype failure

    That's a funny-looking compiler error. What are you compiling that with? Did you write the Keyboard class?

    Is this a copy of the code in your book, or did you add something to some code that worked before?

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: changing datatype failure

    im using eclipse
    oh i forgot to mention that the Keyboard class should be working correctly they instructed me to use it instead of Scanner to get user input

    nope not from the book just tried to follow the instructions for the exercise but when i make it a double the usual way by like this "double total1;" it works
    my teacher recommended notepad because he thins eclipse = cheating, dont know what he meant he hasnt replied yet

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: changing datatype failure

    Can you explain what
    (double)total1/3;
    is doing? What happens if you remove the cast to double?

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: changing datatype failure

    it will calculate the averege (if thats what its called in english) of the 3 intergers the user inputs
    when i make the program in c++ language it seems fine to cast and when i do it in java the way the book wants me to do, its gives me an error
    i would love to encounter this by not using cast but of course i cant because of the instructions given

  10. #10
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: changing datatype failure

    cannot convert from double to int
    If that's an Eclipse error, it's a bit misleading. Java can convert from double to int, it just won't do it implicitly (without you telling it to do it). I think the error message from the ordinary command-line Java compiler is a bit more helpful.
    The expression
    total1/3
    is an int expression - total1 is an int and so is 3. An int divided by an int is an int. The '(double)' tells Java to convert total1 into a double value before dividing it by 3. a double divided by an int is a double (Java *can* convert this way implicitly because no data is lost). You're assigning (with '=') that double result to total2 which is an int. Java can't assign (by converting) a double result to an int variable because double is a floating point type - it might have a non-integer part which would be lost on conversion to an int. There's one explicit data type conversion on that line already, it seems as though you need another one to make that line work.

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: changing datatype failure

    thank you soooo much!! i changed total2 to a double instead of an int and now the cast works correctly! nd my exercise is finished now thanks to you

    heres the small change:

    public class Ex21{
    	public static void main(String args[]){
    		int num1, num2, num3, total1;
    		double total2;
    		num1 = Keyboard.readInt();
    		num2 = Keyboard.readInt();
    		num3 = Keyboard.readInt();
    		total1 = num1+num2+num3;
    		total2 = (double)total1/3;
     
     
    	}
    }

    now i can continue with programming and hope in 10 years i will become a good experienced programmer like you
    thank you once again!

  12. #12
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: changing datatype failure

    I'm glad you believe the exercise is successfully completed. Let's hope your teacher feels the same way

  13. The Following User Says Thank You to Sean4u For This Useful Post:

    fartofagony (September 2nd, 2011)

Similar Threads

  1. Replies: 12
    Last Post: September 3rd, 2011, 07:07 AM
  2. Replies: 1
    Last Post: June 15th, 2011, 01:27 PM
  3. Communications Link Failure
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: June 14th, 2011, 07:03 PM
  4. [SOLVED] JSCH Auth Failure
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2011, 05:17 PM
  5. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM