dimanche 4 octobre 2020

Java: Why is my program outputting the else statement when it's not true?

This program reads user input for example: (this (is (my))input)

and then iterate over the statement adding '(' to Stack s when '(' is encountered and removing '(' from Stack s when ')' is encountered.

Then it checks if there is anything left in the stack and prints a message depending on how many of a particular type of parenthesis are mismatch

// ********************************************************************
// ParenMatch.java
//
// Determines whether or not a string of characters contains
// matching left and right parentheses.
// ********************************************************************

import java.util.*;
import java.util.Scanner;

public class ParenMatch
{
    public static void main (String[] args)
    {
        Stack<Character> s = new Stack<Character>();
        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();

        String goodSoFar = "";


        // Test: Here is a test (which should work (or maybe it doesn't) )

        // loop to process the line one character at a time
        for (int i = 0; i < line.length(); i++) 
        {
            char c = line.charAt(i);
            goodSoFar += c;      // add the character to the string so far

            if( c == '(' )
            {
                // open paren so add it to the stack
                s.push( line.charAt(i) );
            }
            else if ( c == ')' );
            {
                // hit close paren so pull open paren off the stack
                if( s.size() > 0 )
                    s.pop();
                
                else
                {
                    // stack does not have a matching paren so show an error!
                    System.out.println("Error! Close parenthesis without a matching open parenthesis!");
                    System.out.println("Error encountered here: " + goodSoFar + "^");
                }
            }
        }
        
        scan.close();

        // check final stack
        if( s.size() > 0 )
        {
            System.out.println("Error! There are " + s.size() + " extra open parenthesis!");
        }
        else
        {
            System.out.println("The number of open parenthesis matched the number of close parenthesis!");
        }

    }

}

The problem I'm having is that when my input is:

(this (is (my))input)

Then my output is:

Enter a parenthesized expression: (this (is (my))input)
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (t^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (th^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (thi^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this ^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (i^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is ^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (m^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my)^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))i^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))in^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))inp^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))inpu^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))input^
Error! Close parenthesis without a matching open parenthesis!
Error encountered here: (this (is (my))input)^
The number of open parenthesis matched the number of close parenthesis!

But my desired output for that input is:

The number of open parenthesis matched the number of close parenthesis!

Aucun commentaire:

Enregistrer un commentaire