【问题标题】:Validating a string with exceptions [duplicate]验证带有异常的字符串[重复]
【发布时间】:2016-02-04 21:41:08
【问题描述】:

我正在编写一个程序,假设从用户那里读取字符串并验证字符串和操作数。仅有的两个可接受的操作数是“+”和“-”。该字符串不能包含除数字以外的任何字符,如果包含,它应该说“输入错误”但不断提示用户。我在下面粘贴了我的代码,我们假设为这个程序使用异常。我的代码不工作,它崩溃了,这些数字需要总结和打印出来,但我在使用字符串中的操作数时遇到了麻烦

 import java.util.Scanner;

public class Main {


public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String term;

    do {
        String str = input.nextLine();
        term = str.trim();

        try {
            System.out.println(validInput(str));
            System.out.println(sumNums(str));
        } catch (IllegalOperandException e) {
            System.out.println("Bad Input");
        } catch (NumberFormatException e) {
            System.out.println("Bad Input");
        }

    } while (term.length() > 0);

}

public static String validInput(String string) throws IllegalOperandException {
    String output = "";
    String[] stringArray = string.split("\\s+");
    for (String s : stringArray) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!(Character.isDigit(c) || c == '+' || c == '-' || c == '.' )) {
                throw new IllegalOperandException(String.valueOf(c));
            }
            else if(Character.isDigit(c)){
                Double.parseDouble(Character.toString(c));
            }
        }
        output = output + s + " ";
    }
    return output;


}

public static double sumNums (String nums) throws NumberFormatException, ArrayIndexOutOfBoundsException { 
    String[] stringArray2 = nums.split("\\s+"); 
    int i = 0;
    int sum; 

    if (stringArray2[i].equals("-")) { 
        i++;
        sum = Integer.parseInt(stringArray2[i]);    
    } else
        sum = Integer.parseInt(stringArray2[i]); 

    for(int j = 0; j < stringArray2.length; j++) {      

        if (stringArray2[i].equals("+"))    
            sum+=Integer.parseInt(stringArray2[i-1]);
        if (stringArray2[i].equals("-"))
            sum-=Integer.parseInt(stringArray2[i+1]);
    } 
    return sum; 


} 


}

【问题讨论】:

  • 你得到了什么具体的错误?
  • 当我输入一串字符时,它假设说“输入错误”并且它使用户再次提示,但它给了我这个“线程“主”java.lang.Error中的异常:未解决的编译问题:在 TEST.main(TEST.java:21) 处的 TEST.validInput(TEST.java:44) 类型 TEST 的方法 IllegalOperandException(char) 未定义

标签: java arrays string validation exception


【解决方案1】:

首先,要引发异常,您必须创建新对象。所以,正确的做法是

throw new IllegalOperandException(c);

其次,你将一个字符传递给构造函数,但构造函数只能接受String。您可以在 IllegalOperandException 类中创建第二个构造函数

public IllegalOperandException(char c){
    this(String.valueOf(c)); //this will call IllegalOperandException(String) constructor
}

或者你可以改变你抛出异常的行

throw new IllegalOperandException(String.valueOf(c));

第三,return false 不可达。如果你抛出一个异常,代码执行会直接跳转到catch 语句并且你的validInput(String) 不能返回任何东西(它没有返回值的地方)。所以,你不需要它

【讨论】:

  • omg 现在可以工作了,但是现在当我尝试像这样“3231 + sdsa”这样的测试用例时,它假设只说“输入错误”,但输出是“3231 + 错误输入”,我在哪里添加while循环,以便在用户收到“错误输入”消息后,用户可以输入不同的值,程序假设在用户输入空字符串时终止
  • Here 是略微修改的版本。虽然我只会使用正则表达式,但所有检查都可以缩减到 3-4 行
  • 你如何总结像 232 + 10 这样的整数,它会是 242,它会不断提示用户做更多的问题,直到用户输入空格
  • Here's 稍微好一点的版本
  • 我需要总结用户输入的数字,我该怎么做?例如,一个测试用例是这样的“4343.5 + 10 - 15”,它会输出“4348.5”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 2012-10-02
  • 2013-12-30
  • 2021-07-11
相关资源
最近更新 更多