aspose file tools*
The moose likes Java in General and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

Muhamed Senyonjo
Greenhorn

Joined: Mar 13, 2011
Posts: 3
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

Joined: Sep 08, 2007
Posts: 3056
    
  33

You can get the String from the StringBuffer and use the String along with the java.util.Scanner

And welcome to JR


Mohamed Sanaulla | My Blog
Muhamed Senyonjo
Greenhorn

Joined: Mar 13, 2011
Posts: 3
Do we need to cast the StringBuffer into String and then access the scanner class using the casted string refrence;
Muhamed Senyonjo
Greenhorn

Joined: Mar 13, 2011
Posts: 3
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

Joined: Sep 08, 2007
Posts: 3056
    
  33

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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer
 
Similar Threads
Scanner related question of a newbie
scanner properties and Java
overriding hashcode and equals method internal work flow
how to split a text file
concatination of 3 fields in SQL query