Results 1 to 9 of 9
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Error: Type mismatch: cannot convert from java.lang.String to double

    I know it's the double-quotes, but I have no clue what to do.

    // double a, b, c, d, e, f, g, h, i, j, k, l, m

    public class Question4 {
    public static void main(String[] args) {

    double a = 2 + 2 + 3 + 4;
    System.out.println(a);

    double b = "2 + 2" + 3 + 4;
    System.out.println(b);

    double c = 2 + " 2 + 3 " + 4;
    System.out.println(c);

    double d = 3 + 4 + " 2 + 2";
    System.out.println(d);

    double e = "2 + 2 " + (3 + 4);
    System.out.println(e);

    double f = "(2 + 2) " + (3 + 4);
    System.out.println(f);

    double g = "hello 34 " + 2 * 4;
    System.out.println(g);

    double h = 2 + "(int) 2.0" + 2 * 2 + 2;
    System.out.println(h);

    double i = 4 + 1 + 9 + "." + (-3 + 10) + 11 / 3;
    System.out.println(i);

    double j = 8 + 6 * -2 + 4 + "0" + (2 + 5);
    System.out.println(j);

    double k = 1 + 1 + "8 - 2" + (8 - 2) + 1 +1;
    System.out.println(k);

    double l = 5 + 2 + "(1 + 1)" + 4 + 2 * 3;
    System.out.println(l);

    double m = "1" + 2 + 3 + "4" + 5 * 6 + "7" + (8 + 9);
    System.out.println(m);
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    Quote Originally Posted by manibby93 View Post
    I know it's the double-quotes, but I have no clue what to do.
    Remove them! Why are they there anyway?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    The assignment is to "Trace the evaluation of the following expressions, and give their resulting values." I think b means "2 + 234" or something.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    Is this the same exercise you mentioned in Trace the evaluation of expressions, and give their resulting values??? I explained what I thought was meant by "trace" in that thread. Did you add the quotation marks in this exercise to those expressions? You can't "add" Strings to digits and store them in a double.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    No. It is not the same. It's like a two-part exercise. The quotations were already in this one. I am new to DrJava and am trying to figure it out because tutoring is not yet available.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    Interesting! Perhaps the prof wants you to ascertain at what point a compilation error is caught. Unfortunately I don't understand what s/he wants.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    No. She wants me to trace them and get the results. I tried to use backslashes, but I have no clue what to do.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    I think you need to talk to her. The following examples (of which your code has many) won't even compile:

    Java Code:
            double e = "2 + 2 " + (3 + 4);
            System.out.println(e);
     
            double f = "(2 + 2) " + (3 + 4);
            System.out.println(f);
    You cannot add a string and a valid numerical expression together and assign it to a double.
    So either you are confused as to what she wants or she is confused or made a typo. You really need to talk to her about this.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Error: Type mismatch: cannot convert from java.lang.String to double

    I changed it to this and it worked, even though it's probably wrong, but I just need to be able to compile it:

    // double a, b, c, d, e, f, g, h, i, j, k, l, m

    public class Question4 {
    public static void main(String[] args) {

    System.out.println(2 + 2 + 3 + 4);

    System.out.println("2 + 2" + 3 + 4);

    System.out.println(2 + " 2 + 3 " + 4);

    System.out.println(3 + 4 + " 2 + 2");

    System.out.println("2 + 2 " + (3 + 4));

    System.out.println("(2 + 2) " + (3 + 4));

    System.out.println("hello 34 " + 2 * 4);

    System.out.println(2 + "(int) 2.0" + 2 * 2 + 2);

    System.out.println(4 + 1 + 9 + "." + (-3 + 10) + 11 / 3);

    System.out.println(8 + 6 * -2 + 4 + "0" + (2 + 5));

    System.out.println(1 + 1 + "8 - 2" + (8 - 2) + 1 +1);

    System.out.println(5 + 2 + "(1 + 1)" + 4 + 2 * 3);

    System.out.println("1" + 2 + 3 + "4" + 5 * 6 + "7" + (8 + 9));
    }
    }

Similar Threads

  1. Replies: 1
    Last Post: 08-18-2013, 03:52 AM
  2. Type mismatch (String)
    By TSTONESIX in forum New To Java
    Replies: 2
    Last Post: 03-12-2013, 03:13 AM
  3. Type Mismatch error
    By and0rsk in forum New To Java
    Replies: 2
    Last Post: 10-10-2010, 11:16 AM
  4. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  5. Replies: 2
    Last Post: 11-16-2007, 01:24 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
  •