Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error printing on Console !!!!!

 
shankar changayil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test1{
Test1(){}
void method1()
{
System.out.println("method");
}
public static void main(String args[])
{
Test1 t = new Test1();
//line 12. System.out.println ("printing" + t.method1());
}
}
//Here is my doubt ???
At line 12 I am getting the following error...
Incompatible type for +. Can't convert java.lang.String to in System.out.println ("printing" + t.method1());
Can anyone help with this!!!
Thanks in advance.
 
AJ Dussault
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shankar,
you can do a couple of things:
1.
class Untitled1 {
Untitled1() {}
void method1()
{
System.out.println("method");
}
public static void main(String args[])
{
Untitled1 t = new Untitled1();
System.out.println ("printing");
t.method1();
}
}
OR
2.
class Untitled1 {
String method1()
{
return "method";
}
public static void main(String args[])
{
Untitled1 t = new Untitled1();
System.out.println ("printing" + t.method1());
}
}
Amanda
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar
The method returns void. You r not returning anything. You r printing to the console instead. How do u expect it to get concatenated within another print statement.
Try changing the void method to String method and return ("xyz"); instead of the print statement. It should work.
Ankur
 
Uvnik Gupta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
modify method1() as :
String method1()
{
return ("method");
}

 
shankar changayil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amanda, Gupta and Ankur
thanks for the prompt reply
i had tried the t.method1. it worked with the void return type so i was thinking that it should work the same way in the System.out.println as well. As ankur wrote it is not possible at all to print to the console without the return type.
great!!!
shanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic