Java 捕获异常 - 空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26691771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:24:29  来源:igfitidea点击:

Java Catch Exception - Empty String

javatry-catch

提问by Aidan Melen

I'm looking for an exception on how to catch an invalid String that is user input. I have the following code for a exception on an integer input:

我正在寻找有关如何捕获用户输入的无效字符串的异常。对于整数输入的异常,我有以下代码:

            try {
            price = Integer.parseInt(priceField.getText());
            }
            catch (NumberFormatException exception) { 
            System.out.println("price error");
            priceField.setText("");
            break;

But I'm not aware of a specific exception for strings, the input is a simple JTextBox so the only incorrect input I can think of is if the user enters nothing into the box, which is what I'm looking to catch.

但我不知道字符串的特定异常,输入是一个简单的 JTextBox,所以我能想到的唯一不正确的输入是用户没有在框中输入任何内容,这就是我想要捕捉的。

回答by JB Nizet

if (textField.getText().isEmpty())

is all you need.

是你所需要的全部。

Or maybe

或许

if (textField.getText().trim().isEmpty())

if you also want to test for blank inputs, containing only white spaces/tabs.

如果您还想测试仅包含空格/制表符的空白输入。

You generally don't use exceptions to test values. Testing if a string represents an integer is an exception to the rule, because there is no available isInt()method in String.

您通常不使用异常来测试值。测试字符串是否表示整数是该规则的一个例外,因为isInt()String 中没有可用的方法。

回答by Sundar Rajan

You can do like

你可以像

if (priceField.getText().isEmpty()) 
  throw new Exception("priceField is not entered.");

回答by Charlie

You could check if priceFieldcontains a string by using this:

您可以priceField使用以下方法检查是否包含字符串:

JTextField priceField;
int price;
try {
 // Check whether priceField.getText()'s length equals 0
 if(priceField.getText().getLength()==0) {
  throw new Exception();
 }
 // If not, check if it is a number and if so set price
 price = Integer.parseInt(priceField.getText());
} catch(Exception e) {
 // Either priceField's value's length equals 0 or
 // priceField's value is not a number

 // Output error, reset priceField and break the code
 System.err.println("Price error, is the field a number and not empty?");
 priceField.setText("");
 break;
}

When the if-statement is true (If the length of priceField.getText()is 0) an exception gets thrown, which will trigger the catch-block, give an error, reset priceFieldand breakthe code.

当if语句为真时(如果长度priceField.getText()为0)抛出异常,这将触发catch块,给出错误,重置priceFieldbreak代码。

If the if-statement is false though (If the length of priceField.getText()is greater or lower than 0) it will check if priceField.getText()is a number and if so it sets priceto that value. If it not a number, a NumberFormatException gets thrown, which will trigger the catch-block etc.

如果 if 语句为假(如果长度priceField.getText()大于或小于 0),它将检查是否priceField.getText()为数字,如果是,则设置price为该值。如果它不是数字,则会抛出 NumberFormatException,这将触发 catch-block 等。

Let me know if it works.

让我知道它是否有效。

Happy coding :) -Charlie

快乐编码:) -查理

回答by Aidan Melen

if you want your exception to be thrown during the normal operation of the Java Virtual Machine, then you can use this

如果您希望在 Java 虚拟机正常运行期间抛出您的异常,那么您可以使用这个

if (priceField.getText().isEmpty()) 
    throw new RunTimeException("priceField is not entered.");