如何使用扫描仪在java中将输入作为带空格的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39514730/
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 as String with spaces in java using scanner
提问by chandrakanth B
I need to read spaces (present before string and after String) given as input using Scanner Note : if there is no spaces given in input it should not add space in output
我需要使用 Scanner 读取作为输入给出的空格(在字符串之前和字符串之后) 注意:如果输入中没有给出空格,则不应在输出中添加空格
Please find the below code:
请找到以下代码:
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String name= scan.nextLine();
name+=scan.nextLine();
scan.close();
System.out.println("Enter your name"+name);
}
}
I am expecting output like:
我期待输出如下:
Input :Enter Your name:Chandu Aakash
Output:chandu AakashInput: Enter Your name: (Space..)Chandu Aakash(Space..)
Output: (space.. )chandu Aakash(Space..)
输入:输入你的名字:Chandu Aakash
输出:chandu Aakash输入:输入你的名字:(Space..)Chandu Aakash(Space..)
输出:(space..)chandu Aakash(Space..)
采纳答案by esprittn
Your code work fine. I just add little modification:
你的代码工作正常。我只是添加了一点修改:
package practise;
import java.util.Scanner;
public class scanccls {
public static void main(String[] args) {
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name="";
name+=scan.nextLine();
scan.close();
System.out.println("Your name is :"+name);
}
}
回答by Rajesh D
One can use the delimiter function to segregate your input as shown below.
可以使用分隔符功能来分隔您的输入,如下所示。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
String input = scanner.next();
System.out.println(input);
scanner.close();
}
}
回答by Anam
import java.util.*;
public class Str{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
String s=" ";
s= scan.nextLine();
s+=scan.nextLine();
scan.close();
System.out.println("String: "+s);
System.out.println("Double: "+d);
System.out.println("Int: "+i);
}
}
回答by Rezza Prayogi
I use this function below, to read from all user input format, text inclusive spaces, then parse to specific datatype after.
我在下面使用这个函数,从所有用户输入格式中读取,文本包含空格,然后解析为特定的数据类型。
package practice;
import java.io.*;
public class readInputSample{
public static void main(String[] args) {
String strVal = getInput("Enter string value: "); // Direct as string
Integer intVal = Integer.parseInt(getInput("Enter integer value: "));
Double dblVal = Double.parseDouble(getInput("Enter double value: "));
Float fltVal = Float.parseFloat(getInput("Enter float value: "));
System.out.println("String value: " + strVal);
System.out.println("Integer value: " + intVal);
System.out.println("Double value: " + dblVal);
System.out.println("Float value: " + fltVal);
}
// Special Function to read all user input
private static String getInput(String prompt){
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try{
return stdin.readLine();
} catch (Exception e){
return "Error: " + e.getMessage();
}
}
}
回答by Vishal Bhola
package practise;
import java.util.Scanner;
public class scanccls
{
public static void main(String[] args)
{
System.out.println("Enter your name:");
Scanner scan = new Scanner(System.in);
String name = "";
name += scan.nextLine();
// Can also be done like
// String name=scan.next();
// name+=scan.nextLine();
// They Both Work as same
System.out.println("Your name is :" + name);
}
}
回答by Arvind Bakshi
/@esprittn solution didn't work./
/ @espritn 解决方案不起作用。/
my solution:
我的解决方案:
while(scan.hasNext()){
name+=scan.nextLine();
}