扔掉并抓住Java

时间:2016-09-30 17:32:13

标签: java if-statement try-catch throw

我的throw中的if语句在使用“#,$,&,*”时给出了错误的输入错误消息,我无法弄清楚它有什么问题。另外,我需要能够在输入中最多使用99个字符但是我的代码在15左右后停止并且我不知道我编码的错误

public static void main(String[] args) {

    int counter = 1;
    String last = "";
    String output = "";
    String addToOutput = "";


    Scanner s = new Scanner(System.in);

    System.out.print("Enter input string: ");
    String inputstring = s.nextLine();


    System.out.print("Enter flag character: ");
    String inputflag = s.nextLine();


    try{
    if(inputstring != "#" || inputstring != "$" || inputstring != "&" || inputstring != "*"){
            throw new IllegalArgumentException("Bad Input.");


    }
    else{
        System.out.print(output);
    }

    for (int i = 0; i<inputstring.length(); i++){



            if((""+inputstring.charAt(i)).equals(last)){
                counter ++;
    }
        else{
            if(counter>3)
            addToOutput = inputflag + last + counter;
            else{
                addToOutput = "";
                for(int j =0; j<counter; j++){
                    addToOutput = addToOutput + last;
                }

            }
            output = output + addToOutput;
        counter = 1;

    }


    last = "" + inputstring.charAt(i);


            }
        }

    catch(IllegalArgumentException e){
        System.out.println(e.getMessage());
    }

    }

    }

0 个答案:

没有答案