aspose file tools*
The moose likes Beginning Java and the fly likes How to return a String buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to return a String buffer" Watch "How to return a String buffer" New topic
Author

How to return a String buffer

Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
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
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

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.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
Hey Thanqq Roberto

I have changed my method return type and it worked.

Thank you!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return a String buffer