如何在 Java 中获取 StringBuffer 对象的输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26965931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take input for a StringBuffer Object In Java?
提问by Bilal Saqib
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
在下面的代码中接受一个类的对象作为输入。你如何为一个StringBuffer
班级做同样的事情?
String a = new String();
Scanner in = new Scanner(System.in);
System.out.println("Enter a String");
a=in.nextLine();
采纳答案by chrisb2244
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.
将您的“下一行”添加到StringBuffer
sbuffer。
This is because input.nextLine()
returns a String
, and sbuffer.append(...)
accepts a variety of arguments, including String
s.
这是因为input.nextLine()
返回 a String
,并sbuffer.append(...)
接受各种参数,包括String
s。
The documentation for a StringBuffer can be found at this Java page.
StringBuffer 的文档可以在这个 Java 页面找到。
Likewise, scanner documentationis 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.
这些链接提供了每个类可用的方法列表,以及方法可以采用的参数/参数。Java 文档经常提供用例示例。
From the opening paragraph of the StringBuffer
documentation:
从StringBuffer
文档的开头段落:
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.
线程安全的、可变的字符序列。字符串缓冲区类似于字符串,但可以修改。在任何时候,它都包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容。
字符串缓冲区可供多个线程安全使用。这些方法在必要时被同步,以便任何特定实例上的所有操作表现得好像它们以某种串行顺序发生,该顺序与所涉及的每个单独线程进行的方法调用的顺序一致。
回答by Sagar Pudi
Using String string=new String();
is strongly discouraged.
String string=new String();
强烈建议不要使用。
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());
回答by Benjamin
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.
Astringbuffer
类似于 a String
,但可以修改。在任何时候,它都包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容。
Stringbuffers
多线程使用是安全的。这些方法在必要时被同步,以便任何特定实例上的所有操作表现得好像它们以某种串行顺序发生,该顺序与所涉及的每个单独线程进行的方法调用的顺序一致。
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)
.
通常,如果 sb 引用 a 的实例StringBuffer
,则与sb.append(x)
具有相同的效果sb.insert(sb.length(), x)
。
回答by Lawrance
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);
回答by ram sah
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*/
回答by Mahalakshmi
@ 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.
如果您在一行中使用空格分隔符输入所有输入(Int、Float 和 String),则输入的 String 值将分配给 String Buffer 变量。
Below is my console box for your code:
下面是我的代码控制台框:
1 2 Java 5 6.0 HackerWorld Java
1 2 Java 5 6.0 HackerWorld Java
回答by prabhu r
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);
}
}
回答by sharif sarker
String arr;
System.out.println("Enter your word :");
Scanner scan = new Scanner(System.in);
arr= scan.nextLine();
StringBuffer s= new StringBuffer(arr);
回答by Jay Fursule
//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());
}
}