Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*JAVA* Can somebody take a look at my current challenge? I need to show a silent error report when a)(b is entered. It should throw

*JAVA*

Can somebody take a look at my current challenge?

I need to show a silent error report when

a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax"

Other operations that are working and output the following:

x=((a-b)+(c-d) = Encountered error on the left of the expression: Missing right parenthesis

x=(a-b)) = Encountered error on the right of the expression: Missing left parenthesis

x= (((((((((((a-b))))))))))) = Excessive parenthesis combinations

x = (a-b)/(d-v) = Correct parenthesis combinations: match

The code is as follows.

Stack class:

public class ArrayStack {

private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10;

public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0; }

public void push(E value) { if (isFull() == true) { throw new ArrayIndexOutOfBoundsException("Stack is Full"); } else this.arrS[this.size++] = value;

}

public E pop() { if (isEmpty() == true) { throw new ArrayIndexOutOfBoundsException("Stack is Empty"); } return this.arrS[--this.size]; }

public boolean isFull() { return (this.size == MAX_STACK_SIZE) ? true : false;

}

public boolean isEmpty() { return (this.size == 0) ? true : false;

}

public int length() { return this.size; }

public E topValue() { if (isEmpty() == true) { throw new IllegalArgumentException(); } return this.arrS[this.size - 1]; }

public void clear() { this.size = 0;

}

public String show() {

StringBuilder sb = new StringBuilder(); sb.append(" "); for (int i = 0; i < this.size; i++) { sb.append(this.arrS[i]); sb.append(" ");

} sb.append(""); return sb.toString();

}

}

Main driver:

import java.util.Scanner;

public class a3main {

public static void main(String[] args) { int count = 0;

Scanner scan = new Scanner(System.in);

do { System.out.println("Enter an algebraic or arithmetic expression: ");

String arg = scan.nextLine();

ArrayStack stack = new ArrayStack(); boolean flag = true; for (int i = 0; i < arg.length(); i++) { count++; char current = arg.charAt(i);

if (current == '{' || current == '[' || current == '(') { try { stack.push(current); } catch (Exception e) { System.out.println("Excessive parenthesis combinations" + " "); flag = false; break;

} }

if ((current == '}' || current == ')' || current == ']')) { if (stack.isEmpty() == true) { System.out.println( "Encountered error on the right of the expression: Missing left parenthesis" + " "); flag = false; break; } char last = stack.topValue(); if (current == '}' && last == '{' || current == ')' && last == '(' || current == ']' && last == '[') stack.pop(); else {

flag = false;

System.out.println("Mismatch between right and left closing statements " + " "); break; } }

} if (flag) { if (stack.isEmpty() == true) { System.out.println("Correct parenthesis combinations: match" + " "); } else { System.out.println( "Encountered error on the left of the expression: Missing right parenthesis" + " "); } } } while (count != 0); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

describe the main employment rights as stated in the law

Answered: 1 week ago