-2

I am new to Java and trying to finish a program that will read a statement by the user and and scan to see if the amount of LEFT parenthesis match the RIGHT. The person who started the program created a stack but never made any use of it so I left it alone since I'm not very good with stacks. However, I was able to create a loop to to go through every character in the String to find the parenthesis, compare them, then print out if they are even or not. However I am having trouble with the while loop that goes through the String to find all parentheses. It's not working for some reason and I don't understand why. Any explanation on how to make this work will be greatly appreciated.

import java.util.*
public class ParenMatch
       {
public static void main (String[] args)
    {
Stack s = new Stack();
String line; // the string of characters to be checked
Scanner scan = new Scanner(System.in);
System.out.println ("\nParenthesis Matching");
System.out.print ("Enter a parenthesized expression: ");
line = scan.nextLine();

char parenline[] = new char[line.length()];

int x;
while(x < parenline.length) {
parenline[x] = line.charAt(x); 
        x++; 
    }
 int l,r,i,morel,morer = 0; 
while (i > parenline.length) {
        if (parenline[i] == "(" )
            l++;
        if (line.charAt(i) == ")") 
            r++; 
        i++;
    }

    if (l > r) {
        morel = l-r;  
        System.out.println("There are " +morel+ " more left parentheses than    right"); 
    }

    if (r > l) {
        morer = r-l; 
        System.out.println("There are " +morer+ " more right parentheses then left"); 
    }
    if (r == l) {
        System.out.println("The amount of left and right parentheses are even."); 
    }
}

}

Mr. C
  • 43
  • 1
  • 8
  • Define 'not working'. It seems to me that this code won't compile, and for reasons that are perfectly clearly explained in the error messages I think you will get, but it's anybody's guess at the moment. – user207421 Aug 27 '13 at 02:04
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kon Aug 27 '13 at 02:05
  • @Kon I don't think so. – Josh M Aug 27 '13 at 02:12

3 Answers3

0

You need to initialize x so for example.

int x = 0;

You cannot increment an uninitialized variable. Also to define parenline instead of looping and adding the char at the locations in the string just using one of the strings native methods:

char parenline[] = line.toCharArray();

Sorry if i explained this badly.

tom
  • 354
  • 4
  • 15
0

You had following mistakes:

  1. not initializing
  2. using double quote instead of single quote
  3. checking whether i is greather than parenline.length

This is the correct code block:

int x=0;
...

int l,r,i,morel,morer;
l=r=i=morel=morer= 0; 
while (i < parenline.length) {
    if (parenline[i] == '(' )
        l++;
    if (line.charAt(i) == ')') 
        r++; 
    i++;
}
kaisernahid
  • 341
  • 2
  • 13
  • kaisernahid, you were right. I feel like an idiot for missing the initializing and I'm pretty sure I learned that in the conditions for a loop, single quotes are needed. That was a boned-headed move on my part. Thanks for clearing that up. It helped, and my code works now. – Mr. C Aug 27 '13 at 02:35
  • There was enough information in the error messages to tell you all that. – user207421 Aug 27 '13 at 02:38
  • All were compile errors except this one: i > parenline.length. BTW, this will only match total number of parentheses, not their placement as @Vasilina says. – kaisernahid Aug 27 '13 at 02:44
0

I made some changes to your code, it works fine. However, the approach using Stack is better because allows you not only see if the amout of parenthesis is equal, but to see if the expression is correct. For example, if you have something like that: (x+y))+(x-(y+x) then your program can't tell that this is an incorrect expression because the amount of opening and closing parenthesis is equal.

import java.util.*;
public class Stackpr {
    public static void main (String[] args)
    {
    String line; // the string of characters to be checked
    Scanner scan = new Scanner(System.in);
    System.out.println ("\nParenthesis Matching");
    System.out.print ("Enter a parenthesized expression: ");
    line = scan.nextLine();
    int l = 0;
    int r = 0;
    for (int i = 0; i < line.length(); i++){
    if (line.charAt(i) == '(')
        l++;
    else if (line.charAt(i) == ')')
        r++;
        }
    if (l > r) 
    System.out.println("There are " + (l-r) + " more left parentheses than right");
    else if (l < r)
    System.out.println("There are " + (r - l)+ " more right parentheses then left");
    else 
    System.out.println("The amount of left and right parentheses are even.");

}

}