1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java programming error

  1. Sep 23, 2015 #1
    I am programming in Eclipse and I keep seeing the error "
    Type mismatch: cannot convert from int to String", not only in the example I am going to provide but other programs. I've just started programming in Java. Could someone explain to me what this error exactly means?




    publicclassPracOne{

    publicstaticvoidmain(String[] args){


    String s1 = 8;

    String s2 = 8;

    System.out.print(s1.equals(s2));

    }

    }
     
  2. jcsd
  3. Sep 23, 2015 #2

    Krylov

    User Avatar
    Science Advisor
    Education Advisor

    I rarely program in Java, but I suppose you would have to surround 8 by double quotes, on both lines. Otherwise the 8 represents an int(eger), not a string.
     
  4. Sep 23, 2015 #3

    Mark44

    Staff: Mentor

    Please use [ code ] and [ /code ] tags as I have done below.
    When you copy/pasted your code, it ended up with many spaces taken out that should be there. I have added them back in for the first couple of lines.
    I agree with @Krylov. Your two String variables should be initialized with string literals, not int values. Those two lines should look like this:
    Code (Java):

    String s1 = "8";
    String s2 = "8";
     
    Last edited: Sep 23, 2015
  5. Sep 23, 2015 #4
    Oh of course. Because only int can be a real number value, right? And when working with the String constructor it has to be characters or something within quotes?
     
  6. Sep 23, 2015 #5

    Mark44

    Staff: Mentor

    Sort of. It can't have any fractional part.
    Yes
     
  7. Sep 23, 2015 #6
    Is that always true @Mark44? why does the String Constructor have to be within quotes?
     
  8. Sep 23, 2015 #7

    Mark44

    Staff: Mentor

    Is what always true?

    A String literal has to be in double quotes. That's how you know it's a string literal. A character literal (such as 'a') is in single quotes. I don't understand what you're asking about a String Constructor. Your code doesn't explicitly use a constructor for the String class.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Java programming error
  1. Java programming (Replies: 4)

  2. Java compiler error (Replies: 3)

  3. Java program (Replies: 3)

Loading...