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

StringBuffer

 
Muhamed Senyonjo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is their any way i can get user input using the StringBuffer object?

i can usually do it using the scanner class...

Scanner keyboard = new Scanner(System.in)
string name;

System.out.println("what is your name");
name = keyboard.next();

i would like to do the same with stringbuffer.

Thanks for your help.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the String from the StringBuffer and use the String along with the java.util.Scanner

And welcome to JR
 
Muhamed Senyonjo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we need to cast the StringBuffer into String and then access the scanner class using the casted string refrence;
 
Muhamed Senyonjo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i got it....

I used

BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));

String name;

System.out.println("what is your name");
name = br.readLine(); // used the readline to read the input from the BufferedReader

System.out.println("Your name is " + name);

Thanks for your help
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhamed Senyonjo wrote:Do we need to cast the StringBuffer into String and then access the scanner class using the casted string refrence;


A look at the API for StringBuffer would have helped you to find out which method gives the String present in the StringBuffer.

Muhamed Senyonjo wrote: Thanks i got it....

You were doing the same when you used Scanner. Just a different API, And by StringBuffer you actually meant BufferedReader?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:[ . . .And by StringBuffer you actually meant BufferedReader? . . .
I agree. It can be very confusing when you make that sort of mistake in the thread title.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhamed Senyonjo wrote:Do we need to cast the StringBuffer into String and then access the scanner class using the casted string refrence;
You can't cast any of the other classes mentioned here to a String. You have to use the method Mohamed Sanaulla told you to look for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic