【发布时间】:2021-07-28 15:03:09
【问题描述】:
我正在开发一个 .xml 文件生成器。它的工作原理是要求用户输入 5 个不同的值,然后使用它们生成 .xml 数据容器。这是代码:
public class Generator {
public static void main(String[] args) throws InterruptedException {
Scanner reader = new Scanner(System.in);
while (true) {
System.out.println("Input the furni ID (input only numbers here).");
String furniID = reader.nextLine();
System.out.println("Input the furni's file name (without '.swf'. You can input numbers, letters and underscores here).");
String furniFileName = reader.nextLine();
System.out.println("Input the furni's revision (input only numbers here).");
String furniRevision = reader.nextLine();
System.out.println("Input the furni's name (this will be the name that it will display when you click on the furni in a room or in your inventory).");
String furniName = reader.nextLine();
System.out.println("Input the furni's description (this will be displayed under the furni's name).");
String furniDescription = reader.nextLine();
System.out.println("Input the furni's furniline. This is just a name for a group of furnis that belong to the same collection. For example you can input 'custom' (without quotation marks).");
String furniLine = reader.nextLine();
System.out.println("Generating your furnidata...");
System.out.println(" ");
TimeUnit.SECONDS.sleep(1);
System.out.println("<furnitype id=\"" + furniID + "\"" + " classname=\"" + furniFileName + "\"" + ">");
System.out.println("<revision>" + furniRevision + "</revision>");
System.out.println("<xdim>1</xdim>");
System.out.println("<ydim>1</ydim>");
System.out.println("</partcolors>");
System.out.println("<name>" + furniName + "</name>");
System.out.println("<description>" + furniDescription + "</description>");
System.out.println("</adurl");
System.out.println("<offerid>-1</offerid>");
System.out.println("<buyout>0</buyout>");
System.out.println("<rentofferid>-1</rentofferid>");
System.out.println("<rentbuyout>0</rentbuyout>");
System.out.println("<bc>0</bc>");
System.out.println("<excludeddynamic>0</excludeddynamic>");
System.out.println("<customparams/>");
System.out.println("<specialtype>1</specialtype>");
System.out.println("<canstandon>0</canstandon>");
System.out.println("<cansiton>0</cansiton>");
System.out.println("<canlayon>0</canlayon>");
System.out.println("<furniline>" + furniLine + "</furniline>");
System.out.println("</furnitype>");
System.out.println(" ");
System.out.println("Do you want to generate another furnidata? (YES/NO)");
String confirmation = reader.nextLine();
if (confirmation.equals("NO")) {
System.out.println("Furnidata generator has been stopped.");
break;
} else if (confirmation.equals("no")) {
System.out.println("Furnidata generator has been stopped.");
break;
} else if (confirmation.equals("No")) {
System.out.println("Furnidata generator has been stopped.");
break;
}
}
}
我希望程序检测何时有人输入了空字符串,方法是按回车键而不输入任何文字,并打印一条消息,要求用户不要输入空字符串。我使用 try-catch 语句尝试了此操作,但在打印消息后,程序执行下一行,而不是重新执行用户输入空字符串的行。我怎样才能做到这一点?
【问题讨论】:
-
.readLine()如果您提供一个空行,则返回一个空字符串。if (furniLine.isEmpty())例如会检测到这一点。此处无需涉及 try/catch。 -
如果您希望用户在提供空输入时被提示输入,您可以(必须)使用 do-while-loop 来做到这一点。
-
@Seelenvirtuose 好的,我知道 do-while 循环是如何工作的,但我是初学者。如何使“输入字符串不为空”成为
while语句的条件?
标签: java exception java.util.scanner