Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I keep getting errors in my code where it is asking me to input } to end a block although it's not a block. Here's

I keep getting errors in my code where it is asking me to input "}" to end a block although it's not a block.

Here's my code:

import com.sun.tools.javac.parser.Scanner; public class ArrayStack {

public static void main(String[] args) { public ArrayStack() { int a; double[] array = new double[a]; int size = a; int num = 0; } public void push(double a){ int num; int size; if (num < size){ double[] array; array[num] = a; num++; System.out.println("Success"); } else { System.out.println("Failure! Stack is full"); } public double pop(){ int num; if (num > 0){ num--; double[] array; return array[num]; } else { System.out.println("Stack is empty"); return -1; } public boolean isEmpty(){ int num; return (num == 0); } public double peek(int n){ try { int num; if (num > 0) { double[] array; if (n < 0 || n >= num) return -1; else return array[num-1-n]; } else { System.out.println("Stack is empty"); return -1; } } catch(Exception e ){ e.printStackTrace(); } return 0; } public int count(){ int num; return num; } }

class TestArrayStack { public void main(String [] args) { int choice; int pek; double val,poped; boolean empty; Scanner sc =new Scanner(System.in); ArrayStack as = new ArrayStack(); while(true){ System.out.println("1. Enter a Value in stack"); System.out.println("2. Pop a Value"); System.out.println("3. Check If array is Empty"); System.out.println("4. Peek Function"); System.out.println("5. Exit "); choice = sc.nextInt(); switch(choice) { case 1: System.out.print("Enter a value To push : "); val = sc.nextDouble(); as.push(val); break; case 2: poped = as.pop(); System.out.println("Popped : "+poped); break; case 3: empty = as.isEmpty(); System.out.println("Empty ? "+empty); break; case 4: System.out.print("Enter a index to peek : "); pek = sc.nextInt(); poped = as.peek(pek); if(poped != -1) System.out.println("Peeked Value : "+poped); else System.out.println("Oops it was not a valid index this place is empty"); break; case 5: System.exit(0); } } }

} Thank you.

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

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions