0

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

    }

    }
Bbb
  • 117
  • 9
  • you cant compare `strings` like this `if(inputstring != "#" || inputstring != "$" || inputstring != "&" || inputstring != "*")` you should use `equals` method for doing the comparison like `!inputstring.equals("*")` – Rishal Sep 30 '16 at 17:34
  • Also could you please be more specific and clear what do you mean by `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` – Rishal Sep 30 '16 at 17:40
  • @Rishaldevsingh I fixed it but I'm still getting Bad Inputs http://pastebin.com/th8JTNS0 – Bbb Sep 30 '16 at 17:59
  • What exactly you want to do with this code? you can simply use `if ((!inputstring.equals("#")) && (!inputstring.equals("$")) && (!inputstring.equals("&")) && (!inputstring.equals("*")))` instead of so many if condition. Also move your `for (int i = 0; i < inputstring.length(); i++) {` part somewhere inside else part because you are preparing the output inside that and that should get executed only if the condtion is `true` – Rishal Sep 30 '16 at 18:01

0 Answers0