0

For example, we can accept an object of String class as input in the below codes. How do you do the same for a StringBuffer class?

String a = new String();
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
a=in.nextLine();
5
  • 2
    Try StringBuffer a = new StringBuffer(); a.append(scanner.nextLine()); Commented Nov 17, 2014 at 5:17
  • @chrisb2244 ok i got it, its working. but what is the difference between StringBuffer and string? i found a lot of discussion on strings are immutable in Java and StringBuffer are not but i did not get the background of this story, can u help me? Commented Nov 17, 2014 at 5:27
  • Edited my answer to give Java's answer on the reasons for a StringBuffer and some differences with a String. In case you didn't know, 'immutable' just means it can't be changed. A String is created with some value, and this value cannot be modified later. The same is not true for a StringBuffer Commented Nov 17, 2014 at 5:31
  • But we can change a string in java, for example when we concatenate a string with another than we are able to make changes how you can say that Strings are immutable? Commented Nov 17, 2014 at 5:44
  • Because in this case, it's not the same String object. You create a new String object which is made up from the previous two String objects. This is why people often have problems with the stringA == stringB or stringA == "expected contents of stringA", and should instead use stringA.equals(stringB); - == compares the objects, whereas equals(...) compares the contents. For a more in-depth example, see this Commented Nov 17, 2014 at 5:55

9 Answers 9

4

The code:

StringBuffer sbuffer = new StringBuffer();
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
sbuffer.append(input.nextLine());

will add your 'next line' to the StringBuffer sbuffer.

This is because input.nextLine() returns a String, and sbuffer.append(...) accepts a variety of arguments, including Strings.

The documentation for a StringBuffer can be found at this Java page.

Likewise, scanner documentation is also available.

These links provide a list of the methods available for each of these classes, along with the arguments/parameters that methods can take. The Java documentation frequently gives examples of use cases.

From the opening paragraph of the StringBuffer documentation:

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

Sign up to request clarification or add additional context in comments.

Comments

0

Using String string=new String(); is strongly discouraged.

StringBuffer stringbuffer=new StringBuffer(): //you can use StringBuilder here
Scanner input=new Scanner(System.in);
System.out.println("Enter a String");
stringbuffer.append(input.nextLine());

Comments

0

Try This

        Scanner sc = new Scanner(System.in);
        StringBuffer d=new StringBuffer();
        d.insert(0,sc.next());
        d.append(sc.next());

A stringbuffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. Stringbuffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Comments

0

Try this Code

Scanner scanner = new Scanner(System.in);
String a;
System.out.println("Enter a String");
a = scanner.nextLine();
Sytem.out.println("String a value is ::"+a);

Comments

0

@ Ram,

If you are entering all inputs (Int, Float and String) in a single line with space delimiter, then the entered String value will be assigned to String Buffer variable.

Below is my console box for your code:

1 2 Java 5 6.0 HackerWorld Java

Comments

0
import java.io.*;
public class Cutter
{
public static void main(String args[]) throws IOException
{
    System.out.println("enter a string");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str=br.readLine();
    StringBuffer strbfr=new StringBuffer(str);

}
}

1 Comment

This question has already been answered in the comments section.
0
//program of revrse a string using string buffer inbuild functions
import java.util.*;
class RevrseByStringBuffer
{   
    public static void main(String args[])
    {
        StringBuffer s1 = new StringBuffer();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a String: ");
        s1.append(scan.nextLine());
        System.out.println("Revrse String is: "+s1.reverse());
    }
}

Comments

-1
Scanner sc=new Scanner(System.in);
String s="HackerWorld";
int i=4;
double d=4.0;
int i2;
double d2;


i2=sc.nextInt();

d2=sc.nextDouble();
StringBuffer s1= new StringBuffer();
s1.append(sc.nextLine());
System.out.println(i+i2);
System.out.println(d+d2);
System.out.println(s+" "+s1);
/*It doesn't take input in buffer string.So i couldn't not able to append two strings.Plz help me to solve this*/

Comments

-1
    String arr;

    System.out.println("Enter your word :");
    Scanner scan = new Scanner(System.in);
    arr= scan.nextLine();
    StringBuffer s= new StringBuffer(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.