Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to return a String buffer

 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone..

I am new to java.I am writing a program wherein I have to return a stringbuffer using a switch case in a method.



So when I call the method i.e. getTypeDescr(SINGLE_FAMILY) it should return SingleFamily. Here in the above code I want to use a String buffer instead of a String to return the value.

But when I say return str it gave me error saying

C:\Java\Lesson1>javac HomeType1.java
HomeType1.java:25: incompatible types
found : java.lang.StringBuffer
required: java.lang.String
return str;

Can someone please let me know how to return a String buffer and what mistake I am doing??

Thank you!!


[HENRY: Fixed code tags -- and enabled]
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divya Kotamraju wrote:
Can someone please let me know how to return a String buffer and what mistake I am doing??


Did you forget to declare that the method now returns a string buffer?

Henry
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Divya!

Champ, the problem is that you are trying to return a StringBuffer and the return type of the getTypeDescr method is String. A StringBuffer is not a String. This would work if StringBuffer extended String, but this isn't possible because the String class is final, and thus, nobody can extend it.

What you can do is change the return type of your method to return StringBuffer instead of String, or return str.toString() instead of only str.
 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thanqq Roberto

I have changed my method return type and it worked.

Thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic