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();
chrisb2244
  • 2,940
  • 22
  • 44
Bilal Saqib
  • 87
  • 1
  • 1
  • 10
  • 2
    Try `StringBuffer a = new StringBuffer(); a.append(scanner.nextLine());` – chrisb2244 Nov 17 '14 at 05: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? – Bilal Saqib Nov 17 '14 at 05: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` – chrisb2244 Nov 17 '14 at 05: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? – Bilal Saqib Nov 17 '14 at 05: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](http://stackoverflow.com/questions/767372/java-string-equals-versus) – chrisb2244 Nov 17 '14 at 05:55

9 Answers9

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.

Community
  • 1
  • 1
chrisb2244
  • 2,940
  • 22
  • 44
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());
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
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).

Benjamin
  • 2,257
  • 1
  • 15
  • 24
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);
Lawrance
  • 975
  • 3
  • 9
  • 17
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

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);

}
}
prabhu r
  • 233
  • 2
  • 10
  • 16
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());
    }
}
Szántó Zoltán
  • 981
  • 1
  • 12
  • 26
-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*/
-1
    String arr;

    System.out.println("Enter your word :");
    Scanner scan = new Scanner(System.in);
    arr= scan.nextLine();
    StringBuffer s= new StringBuffer(arr);
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72