[英]How to take input as String with spaces in java using scanner
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我需要使用扫描仪读取作为输入给出的空格(在字符串之前和字符串之后)注意:如果输入中没有空格,则不应在 output 中添加空格
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:我期待 output 像:
Input:Enter Your name:Chandu Aakash输入:输入你的名字:Chandu Aakash
Output:chandu Aakash Output:chandu Aakash
Input: Enter Your name: (Space..)Chandu Aakash(Space..)输入:输入您的姓名:(Space..)Chandu Aakash(Space..)
Output: (space.. )chandu Aakash(Space..) Output:(空格..)chandu Aakash(空格..)
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);
}
}
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();
}
}
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);
}
}
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();
}
}
}
/ @esprittn solution didn't work. / @espritn 解决方案不起作用。 / /
my solution:我的解决方案:
while(scan.hasNext()){
name+=scan.nextLine();
}
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);
}
}
I have done a few changes in your code, it will run just fine for this code我对您的代码进行了一些更改,此代码可以正常运行
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);
}
}
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.