cnmesr
cnmesr

Reputation: 345

Slowly trying to code something in java that will say if insertion of brackets is correct (arithmetic)

You type something in the console, let's say (8+8). Then the program will tell you if the insertion of brackets is correct.

Here is my (of course not finished) definition of wrong brackets:

() this means if one array element is ( and next )
)( 
if the amount of "(" is not equal to amount of ")"

else we have correct brackets.

What my code does is it splits the console input into String array elements. And I work with these. The problem is I cannot write a statement where I get an output True when we have (8), as example.

Here is my code:

    import java.util.Scanner;
    public class Brackets{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String any = input.nextLine();
        String sArray[] = any.split("");
        int counter1=0;
        int counter2=0;


        for(int i=0; i<sArray.length-1; i++){
            if(sArray[i].equals("(")){
                counter1++;
            }
            if(sArray[i].equals(")")){
                counter2++;
            }
            if(sArray[i].equals("(") && sArray[i+1].equals(")")){
                System.out.println("False");
                return;
            }
            if(sArray[i].equals(")") && sArray[i+1].equals("(")){
                System.out.println("False");
                return;
            }
        }
        if(counter1 == counter2){
            System.out.println("True");
        }
        if(counter1 != counter2){
            System.out.println("False");
        }
    }
}

I think there is a problem with this

if(sArray[i].equals("(") && sArray[i+1].equals(")")){
                System.out.println("False");
                return;
            }

But how can I fix that if I'm looking for 2 neighbor array elements such as ()

Upvotes: 0

Views: 47

Answers (2)

Tim B
Tim B

Reputation: 41208

These checks are both wrong and redundant.

        if(sArray[i].equals("(") && sArray[i+1].equals(")")){
            System.out.println("False");
            return;
        }
        if(sArray[i].equals(")") && sArray[i+1].equals("(")){
            System.out.println("False");
            return;
        }

You can also simplify things a lot, you only need one counter for example.

  int counter = 0;
  for(int i=0; i<sArray.length-1; i++){
        if(sArray[i].equals("(")){
            counter++;
        }
        if(sArray[i].equals(")")){
            counter--;
        }
        if (counter<0) {
          System.out.println("Close bracket with no open bracket found");
        }
    }

    if (counter > 0) {
        System.out.println("An open bracket was never closed");
    }

This has a number of improvements.

  1. Meaningful messages on what it found
  2. Detects a close bracket without an open even if there is a later open. For example ")(" would fail but "()" pass.
  3. Only uses one counter variable

The last change is stylistic really. You could still do this with two counters and compare the second one to the first rather than comparing with zero. The end result is the same though.

One more improvement, there is no need to split the string, and else if will be a tiny bit more efficient as we know it can't be a ')' if it was a '('.

  int counter = 0;
  for(int i=0; i<any.length()-1; i++) {
        if(any.charAt(i) == '(') {
            counter++;
        } else if(any.charAt(i) == ')')) {
            counter--;
        }
        if (counter<0) {
          System.out.println("Close bracket with no open bracket found");
        }
    }

    if (counter > 0) {
        System.out.println("An open bracket was never closed");
    }

This will be far more efficient as no need to do all the string splitting etc. All method names are from memory so may need a bit of tweaking.

Upvotes: 3

Patrick Parker
Patrick Parker

Reputation: 4967

You only need one counter to count the number of opened scopes. Then if it goes negative at any point, Fail. Then if it is zero at the end, Pass, otherwise Fail.

Upvotes: 2

Related Questions