如何使用扫描仪在java中将输入作为带有空格的字符串
Posted
技术标签:
【中文标题】如何使用扫描仪在java中将输入作为带有空格的字符串【英文标题】:How to take input as String with spaces in java using scanner 【发布时间】:2016-09-15 15:17:55 【问题描述】:我需要使用扫描仪读取作为输入的空格(存在于字符串之前和字符串之后) 注意:如果输入中没有空格,则不应在输出中添加空格
请找到以下代码:
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);
我期待这样的输出:
输入:输入您的姓名:Chandu Aakash 输出:chandu Aakash
输入:输入您的姓名:(Space..)Chandu Aakash(Space..) 输出:(space..)chandu Aakash(Space..)
【问题讨论】:
【参考方案1】:您的代码运行良好。我只是添加一点修改:
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);
【讨论】:
【参考方案2】:您可以使用分隔符功能来分隔您的输入,如下所示。
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();
【讨论】:
【参考方案3】: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);
【讨论】:
【参考方案4】:我在下面使用这个函数,读取所有用户输入格式,包含文本的空格,然后解析为特定的数据类型。
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();
【讨论】:
【参考方案5】:/@esprittn 解决方案不起作用。/
我的解决方案:
while(scan.hasNext())
name+=scan.nextLine();
【讨论】:
【参考方案6】:我对你的代码做了一些改动,这段代码运行得很好
package practise;
import java.util.Scanner;
public class scanccls
public static void main(String[] args)
//// TODO Auto-generated method stub
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);
【讨论】:
【参考方案7】: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);
【讨论】:
【参考方案8】: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();
scan.close();
System.out.println("Enter your name"+name);
【讨论】:
以上是关于如何使用扫描仪在java中将输入作为带有空格的字符串的主要内容,如果未能解决你的问题,请参考以下文章