我需要提供单独的异常语句。 1. 空字符串和 2. 有效数字数据

发布于 2024-11-02 18:56:14 字数 856 浏览 2 评论 0原文

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(" Enter the Amount of articles to be ordered.");
            amount = reader.readLine();

            if(amount.trim().isEmpty()){
                System.out.println("Amount Entered is Empty");
            }

            for(int count=0;count<amount.length();count++){
                if(!Character.isDigit(amount.charAt(count))){
                    throw new NumberFormatException();
                }
            }            
            order.validateAmount(amount);
        }catch(NumberFormatException numbere){
            System.out.println("Either Number format is uncorrect or String is Empty, Try Again.");
    }

上面的代码为我提供了针对空字符串异常和无效数字数据异常的单个 println() 语句,这是我不想要的。我想要两个异常的单独 println() 语句。怎么得到?

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(" Enter the Amount of articles to be ordered.");
            amount = reader.readLine();

            if(amount.trim().isEmpty()){
                System.out.println("Amount Entered is Empty");
            }

            for(int count=0;count<amount.length();count++){
                if(!Character.isDigit(amount.charAt(count))){
                    throw new NumberFormatException();
                }
            }            
            order.validateAmount(amount);
        }catch(NumberFormatException numbere){
            System.out.println("Either Number format is uncorrect or String is Empty, Try Again.");
    }

The above code gives me single println() statement for both empty string exception and invalid numeric data exception, which I don't want. I want separate println() statements for both exception. how to get?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┈┾☆殇 2024-11-09 18:56:14
  1. 您可以使用两个不同的异常,例如NumberFormatExceptionIllegalArgumentException,并执行两个不同的catch-子句

    <前><代码> ...
    if (金额.isEmpty())
    抛出新的 IllegalArgumentException();
    ...

    } catch (NumberFormatException numbere) {
    System.out.println("任一数字格式不正确,请重试。");
    } catch (IllegalArgumentException 空) {
    System.out.println("字符串为空,请重试。");
    }

  2. 使用相同的异常,但具有不同的消息

    <前><代码>尝试{
    BufferedReader 阅读器 = 新 BufferedReader(新 InputStreamReader(
    系统.in));
    System.out.println("请输入要订购的商品数量。");
    字符串数量 = reader.readLine();

    if (amount.trim().isEmpty()) {
    System.out.println("输入的金额为空");
    }

    if (金额.isEmpty())
    throw new IllegalArgumentException("字符串为空。");

    for (int count = 0; count < amount.length(); count++)
    if (!Character.isDigit(amount.charAt(count)))
    throw new IllegalArgumentException("数字格式不正确。");

    order.validateAmount(金额);
    } catch (IllegalArgumentException e) {
    System.out.println(e.getMessage() + " 再试一次。");
    }

  3. 或者,您可以推出自己的异常两个不同的构造函数,以及一个标志,说明异常是否是由于错误的数字或空字符串引起的。

  1. You could either use two different exceptions, for instance NumberFormatException and IllegalArgumentException and do two different catch-clauses.

        ...
        if (amount.isEmpty())
            throw new IllegalArgumentException();
        ...
    
    } catch (NumberFormatException numbere) {
        System.out.println("Either Number format is uncorrect, Try Again.");
    } catch (IllegalArgumentException empty) {
        System.out.println("String is empty, Try Again.");
    }
    
  2. Use the same exception but with different messages:

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the Amount of articles to be ordered.");
        String amount = reader.readLine();
    
        if (amount.trim().isEmpty()) {
            System.out.println("Amount Entered is Empty");
        }
    
        if (amount.isEmpty())
            throw new IllegalArgumentException("String is empty.");
    
    
        for (int count = 0; count < amount.length(); count++)
            if (!Character.isDigit(amount.charAt(count)))
                throw new IllegalArgumentException("Number format incorrect.");
    
        order.validateAmount(amount);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage() + " Try again.");
    }
    
  3. Or, you could roll your own Exception with two different constructors, and a flag stating if the exception was due to a bad number or an empty string.

我的鱼塘能养鲲 2024-11-09 18:56:14

由于空字符串是一个“预期”异常,我不会使用异常,而是检查它:

if ( amount.trim().equals( string.empty) )
{
   System.out.println("string empty" );
}
else
{
   //do your other processing here
}

另一个空字符串检查是 amount.trim().length == 0

如果你真的想使用异常:

if( amount.trim().equals( string.empty) )
{
   throw new IllegalArgumentException( "Amount is not given" );
}

并添加另一个 catch()

}
catch( NumberFormatException numbere)
{
}
catch( IllegalArgumentException x )
{
  // Amount not given
}

Since an empty string is an 'expected' exception I would not use an exception but check for it:

if ( amount.trim().equals( string.empty) )
{
   System.out.println("string empty" );
}
else
{
   //do your other processing here
}

Another check for emptiness would be amount.trim().length == 0

If you really want to use exceptions:

if( amount.trim().equals( string.empty) )
{
   throw new IllegalArgumentException( "Amount is not given" );
}

and add another catch()

}
catch( NumberFormatException numbere)
{
}
catch( IllegalArgumentException x )
{
  // Amount not given
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文