简体   繁体   English

需要一些指导,以参考Java中扫描程序生成的字符串中的空格

[英]need some guidance in reference to spaces in strings generated from scanner in java

My string description crashes the Console because of the spaces generated in normal sentence structure. 由于正常句子结构中生成的空格,我的字符串描述使控制台崩溃。 i am looking for some guidance in reference to why this happens and futhermore if i am going about this the wrong way how should i be approaching it. 我正在寻找有关为什么会发生这种情况的指导,此外,如果我要以错误的方式处理此问题,我应该如何处理。

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    System.out.println("-/- Job ticket  -/-");
    Scanner MyScanner = new Scanner (System.in);

    String FirstName;
    String LastName;
    String Phone;
    String Address;
    String Description;
    int ticketNumber;
    int orderMonth ;
    int orderDay;
    int orderYear;
    int requestedDay;
    int requestedMonth;
    int requestedYear;

    System.out.println("First Name = ?");
    FirstName = MyScanner.next();
    System.out.println("Last Name = ?");
    LastName = MyScanner.next();
    System.out.println("Phone = ?");
    Phone = MyScanner.next();
    System.out.println("Address = ?");
    Address = MyScanner.next();
    System.out.println("Description = ?");
    Description = MyScanner.next();
    System.out.println("Ticket = ?");
    ticketNumber = MyScanner.nextInt();
    System.out.println("Order Date Month = ?");
    orderMonth = MyScanner.nextInt();
    System.out.println("Order Date Day");
    orderDay = MyScanner.nextInt();
    System.out.println("Order Date Year");
    orderYear = MyScanner.nextInt();
    System.out.println("Requested Date Month");
    requestedMonth = MyScanner.nextInt();
    System.out.println("Requested Date Day");
    requestedDay = MyScanner.nextInt();
    System.out.println("Requested Date Year");
    requestedYear = MyScanner.nextInt();

    System.out.println("=====================================================");
    System.out.print("Ticket :   ");
    System.out.println(ticketNumber);
    System.out.print("Customer:   ");
    System.out.print(FirstName);
    System.out.print(" ");
    System.out.println(LastName);
    System.out.print("Home Phone: ");
    System.out.println(Phone);
    System.out.print("Order Date: ");
    System.out.print(orderMonth);
    System.out.print('/');
    System.out.print(orderDay);
    System.out.print('/');
    System.out.println(orderYear);
    System.out.print("Requested Date:   ");
    System.out.print(requestedMonth);
    System.out.print('/');
    System.out.print(requestedDay);
    System.out.print('/');
    System.out.println(requestedYear);
    System.out.println("-----------------------------------------------------");
    System.out.println("Address");
    System.out.println(Address);
    System.out.println("-----------------------------------------------------");
    System.out.println("Description");
    System.out.println(Description);
    System.out.println("=====================================================");
    System.out.println();
   }
}

I see a problem in your code here. 我在这里在您的代码中看到一个问题。

When you use Scanner.next(), Scanner looks for the next whitespace " " and returns you all the text before that whitespace. 当您使用Scanner.next()时,扫描程序将查找下一个空格“”,并向您返回该空格之前的所有文本。

This is due to the concept of delimiters in the Scanner class and the default delimiter is a 'whitespace or next line' (You might want to read up on delimiters). 这是由于Scanner类中的定界符的概念所致,默认定界符为“空白或下一行”(您可能需要阅读定界符)。

How does the delimiter work for you? 分隔符如何为您工作?

Eg: Your input is "Hello World\\r\\n" 例如:您输入的是“ Hello World \\ r \\ n”

When you invoke Scanner.next(), Scanner will find the whitespace, and return you everything before that, so it will return "Hello". 当您调用Scanner.next()时,Scanner将找到空白,并在此之前返回所有内容,因此它将返回“ Hello”。 When you invoke Scanner.next() again, Scanner will find the \\r\\n (next line) and return you everything before that, so it will return "World". 当您再次调用Scanner.next()时,Scanner将找到\\ r \\ n(下一行)并返回之前的所有内容,因此它将返回“世界”。

Scanner will only prompt for new user input when it has finished reading all the input. 扫描程序仅在读取完所有输入后才提示输入新的用户输入。

So for your code, what will happen is that: 因此,对于您的代码,将发生以下情况:

Address = MyScanner.next();
System.out.println("Description = ?");
Description = MyScanner.next();
System.out.println("Ticket = ?");

If my input for Address is "123 ABC Avenue", Address will only get the input "123", while Description will automatically pull the value "ABC" from the input which is not empty yet. 如果我对Address的输入是“ 123 ABC Avenue”,则Address只会得到输入“ 123”,而Description将自动从尚未为空的输入中提取值“ ABC”。

Thus, you will observe that the program appears to "skip" the Description user prompt. 因此,您将观察到该程序似乎“跳过” Description用户提示。

You should use the Scanner.nextLine() method if you want to pull the entire user input out from the Scanner, instead of Scanner.next() 如果要从扫描仪中拉出整个用户输入,而不是Scanner.next(),则应使用Scanner.nextLine()方法。

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

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