To input data in Java using a StringBuffer
, you can use the BufferedReader
class.
Here is an example of how to use the BufferedReader
class to input a string and append it to a StringBuffer
:
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a string: "); StringBuffer sb = new StringBuffer(); sb.append(br.readLine()); System.out.println("You entered: " + sb); br.close(); } }
In this example, the BufferedReader
class is used to read a line of input from the console. The readLine()
method is used to read the line as a String
, and the append()
method is used to append the String
to the StringBuffer
. The StringBuffer
is then printed to the console.
For more information on the BufferedReader
class and the StringBuffer
class in Java, you can refer to the Java tutorial and the Java documentation.