Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Would like this done in JAVA: The professor wants it to be done with a for loop and this is what he gave us to

Would like this done in JAVA:

The professor wants it to be done with a "for loop" and this is what he gave us to work with: (Just don't know what else to add to the loop)

Now, on the line that requires the loop, create a for loop to process each character -- one character at a time.

for( int i = 0; i < line.length(); i++ ) { // get the current character to process char current = line.charAt(i); // if we hit an opening parenthesis, put it on the stack and then stop processing this character -- move to the next loop cycle (iteration) if( current == '(' ) { s.push(current); continue; // skip the rest of the processing for this loop iteration and move to the next iteration (cycle) }

// if you hit a closing parenthesis, do you have a match on the stack? If so, pop it off the stack. If not, display an error message and the line up to this point (substring) and stop the loop (break) }

Please notice that you only have to match up the parenthesis. You do not have to match up any other brackets unless you want to do so.

Matching Parentheses

One application of stacks is to keep track of things that must match up such as parentheses in an expression or braces in a program. In the case of parentheses when a left parenthesis is encountered it is pushed on the stack and when a right parenthesis is encountered its matching left parenthesis is popped from the stack. If the stack has no left parenthesis, that means the parentheses dont matchthere is an extra right parenthesis. If the expression ends with at least one left parenthesis still on the stack then again the parentheses dont matchthere is an extra left parenthesis.

File ParenMatch.java contains the skeleton of a program to match parentheses in an expression. It uses the Stack class provided by Java (in java.util). Complete the program by adding a loop to process the line entered to see if it contains matching parentheses. Just ignore characters that are neither left nor right parentheses. Your loop should stop as soon as it detects an error. After the loop print a message indicating what happened the parentheses match, there are too many left parentheses, or there are too many right parentheses. Also print the part of the string up to where the error was detected.

// ******************************************************************** // 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 s = new Stack(); String line; // the string of characters to be checked Scanner scan = new Scanner(System.in);

System.out.println (" Parenthesis Matching"); System.out.print ("Enter a parenthesized expression: "); line = scan.nextLine(); // loop to process the line one character at a time for(int i=0; i

//if we hit an opening parenthesis, put it on the //stack and then stop processing this character -- //move to the next loop cycle (iteration) if(current =='(') { s.push(current); continue; //skip the rest of the processing for this loop iteration and move // to the next iteration (cycle) } } // print the results } }

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_2

Step: 3

blur-text-image_3

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

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago