Java programming error

  • #1
248
3
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));

}

}
 

Answers and Replies

  • #2
S.G. Janssens
Science Advisor
Education Advisor
958
728
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.
 
  • #3
35,028
6,775
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 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?



Java:
public class PracOne{

   public static void main(String[] args)
   {
      String s1 = 8;
      String s2 = 8;
      System.out.print(s1.equals(s2));
   }
}
I agree with @Krylov. Your two String variables should be initialized with string literals, not int values. Those two lines should look like this:
Java:
String s1 = "8";
String s2 = "8";
 
Last edited:
  • #4
248
3
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?
 
  • #5
35,028
6,775
Oh of course. Because only int can be a real number value, right?
Sort of. It can't have any fractional part.
Niaboc67 said:
And when working with the String constructor it has to be characters or something within quotes?
Yes
 
  • #6
248
3
Is that always true @Mark44? why does the String Constructor have to be within quotes?
 
  • #7
35,028
6,775
Is that always true @Mark44? why does the String Constructor have to be within quotes?
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.
 

Related Threads on Java programming error

  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
2
Views
4K
Replies
7
Views
2K
Replies
11
Views
2K
Replies
9
Views
2K
Replies
1
Views
1K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
12
Views
8K
Top