简体   繁体   English

如何在所有包含空格的位置扫描多个字符串?

[英]How do you scan multiple strings in all containing spaces?

I'm attempting to scan in strings one after another and some may contain spaces. 我试图一个接一个地扫描字符串,其中一些可能包含空格。 As my code functions currently it will only work if each string you type in is one word. 由于我的代码目前处于功能状态,因此只有在您输入的每个字符串都是一个单词的情况下,它才起作用。 What's the proper way to scan in consecutive lines of text, regardless of there being spaces in the line or not. 在连续的文本行中进行扫描的正确方法是什么,而不管行中是否有空格。

System.out.print("Enter your first name: ");
                fName = scan.next();

                System.out.print("Enter your last name: ");
                lName = scan.next();

                System.out.print("Enter your street and house number: ");
                address = scan.next();

                System.out.print("Enter your city: ");
                city = scan.next();

                System.out.print("Enter your state: ");
                state = scan.next();

                System.out.print("Enter your telephone number (no spaces): ");
                teleNum = scan.next();

                System.out.print("Enter your zip code: ");
                zip = scan.next();
    String[] prompts = {
        "Enter your city: ",
        "Enter your state: ",
    };

    Scanner scanner = new Scanner(System.in);
    String line = "";
    for (String prompt : prompts) {
        System.out.println(prompt);
        if (scanner.hasNextLine()) { 
            line += scanner.nextLine();
        }
    }

    System.out.println(line);

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

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