1

由于正常句子结构中生成的空格,我的字符串描述使控制台崩溃。我正在寻找一些关于为什么会发生这种情况的指导,此外,如果我以错误的方式解决这个问题,我应该如何处理它。

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();
   }
}
4

1 回答 1

0

我在这里看到你的代码有问题。

当您使用 Scanner.next() 时,Scanner 会查找下一个空格 " " 并返回该空格之前的所有文本。

这是由于 Scanner 类中的定界符的概念,默认定界符是“空白或下一行”(您可能需要阅读定界符)。

分隔符如何为您工作?

例如:您的输入是“Hello World\r\n”

当您调用 Scanner.next() 时,Scanner 会找到空白,并返回之前的所有内容,因此它会返回“Hello”。当您再次调用 Scanner.next() 时,Scanner 将找到 \r\n (下一行)并返回之前的所有内容,因此它将返回“World”。

扫描仪仅在读取完所有输入后才会提示新的用户输入。

因此,对于您的代码,将会发生什么:

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

如果我的地址输入是“123 ABC Avenue”,地址只会得到输入“123”,而描述会自动从输入中提取值“ABC”,该输入还不是空的。

因此,您将观察到程序似乎“跳过”了描述用户提示。

如果要从 Scanner 中提取整个用户输入,则应使用 Scanner.nextLine() 方法,而不是 Scanner.next()

于 2012-06-08T03:17:14.290 回答