简体   繁体   English

如何使用扫描仪跳过空格并在单行中输入

[英]How to skip whitespaces and take input in Single line using Scanner

I need to take input in a single line of String which is a combination of String and integers.我需要在一行字符串中输入,它是字符串和整数的组合。

I have tried to take the input like this我试图接受这样的输入

Scanner in = new Scanner(System.in);
String stringWithoutSpaces=in.nextLine();

But, scanner reads only the first character.但是,扫描仪只读取第一个字符。

Input String:A 10,B 10,C 10,D 10
Required String:A10,B10,C10,D10 

I need this input in a one single line using scanner class.我需要使用扫描仪类在一行中输入此输入。

From what I understand, you are trying to receive the string from the user without storing all the white spaces.据我了解,您试图在不存储所有空格的情况下从用户接收字符串。 If your goal is to remove all the white spaces from the user input string, you can use replaceAll .如果您的目标是从用户输入字符串中删除所有空格,则可以使用replaceAll

Code:代码:

Scanner in = new Scanner(System.in);
System.out.print("Enter input: ");
String input = in.nextLine();
String noWhiteSpaces = input.replaceAll(" ", "");
System.out.println(noWhiteSpaces);

Console:安慰:

Enter input: A 10,B 10,C 10,D 10
A10,B10,C10,D10
    import java.util.*;

public class Solution {

    public static void main(String args[]) {
        System.out.println("Hello world");
        Scanner scanner=new Scanner(System.in);
        String aItems = scanner.nextLine();
        System.out.println(aItems);

    }
}

This is working fine.这工作正常。 I have Provided same input as mentioned by you and it is taking it as one string.Share your complete code or match from this one.我提供了与您提到的相同的输入,并将其作为一个字符串。分享您的完整代码或匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM