vendredi 30 septembre 2016

Throw and catch Java

The if statement in my throw is gives a bad input error message when using "#,$,&,*" and I can't figure out what is wrong with it. In addition, I need to able able to take up to 99 characters in the input but my code stops after around 15 and I don't know what I coded wrong

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());
    }

    }

    }

Aucun commentaire:

Enregistrer un commentaire