There is another error in "TestIntegerStackWithExceptions.java"

There are two errors in "TestIntegerStackWithExceptions.java". Below are the classes to help you to correct.

/**

* A class to represent the negative size exception. (NegativeSizeException.java)

*

* @author Lam Chou Yin @ Edwin

* @version 1.0.0 18 October 2004

*/

publicclass NegativeSizeExceptionextends Exception

{

/**

* Constructor without parameter.

*/

public NegativeSizeException()

{

super("cannot set a negative size");

}

/**

* constructor with parameter.

* @param message message of the exception.

*/

public NegativeSizeException(String message)

{

super(message);

}

}

/**

* A class to represent the invalid stack position exception. (InvalidStackPositionException.java)

*

* @author Lam Chou Yin @ Edwin

* @version 1.0.0 18 October 2004

*/

publicclass InvalidStackPositionExceptionextends Exception

{

/**

* Constructor without parameter.

*/

public InvalidStackPositionException()

{

super("an invalid stack position was provided");

}

/**

* Constructor with parameter.

* @param message message of the exception.

*/

public InvalidStackPositionException(String message)

{

super(message);

}

}

/**

* A class to represent <code>IntegerStackWithExceptions</code> object. (IntegerStackWithExceptions.java)

*

* @author Lam Chou Yin @ Edwin

* @version 1.0.0 2 October 2004

*/

publicclass IntegerStackWithExceptions

{

/**

* To hold the stack of integers.

*/

privateint[] stack;

/**

* To track number of items.

*/

privateint total;

/**

* A constructor with one argument.

*/

public IntegerStackWithExceptions(int sizeIn)throws NegativeSizeException

{

if (sizeIn < 0)

{

thrownew NegativeSizeException();

}

else

{

stack =newint[sizeIn];

total = 0;

}

}

/**

* Add an item to the array.

* @return true when the item is successfully added.

*/

publicboolean push(int j)

{

if (isFull() ==false)// checks if space in stack

{

stack[total] = j;// add item

total++;// increment item counter

returntrue;// to indicate success

}

else

{

returnfalse;// to indicate failure

}

}

/**

* Remove an item by obeying LIFO rule.

* @return true when the item is successfully removed.

*/

publicboolean pop()

{

if (isEmpty() ==false)// makes sure stack is not empty

{

total--;// reduce counter by one

returntrue;// to indicate success

}

else

{

returnfalse;// to indicate failure

}

}

/**

* Checks if array is empty.

* @return true when an array is empty.

*/

publicboolean isEmpty()

{

if (total == 0)

{

returntrue;

}

else

{

returnfalse;

}

}

/**

* Checks if array is full.

* @return true when an array is full.

*/

publicboolean isFull()

{

if (total == stack.length)

{

returntrue;

}

else

{

returnfalse;

}

}

/**

* Returns the ith item.

* @return the ith item.

*/

publicint getItem(int i)throws InvalidStackPositionException

{

if (i < 1 || i > total)

{

thrownew InvalidStackPositionException();

}

else

{

return stack[i - 1];// ith item at position i - 1

}

}

/**

* Return the number of items in the array.

* @return the number of items in the array.

*/

publicint getTotal()

{

return total;

}

}

// EasyIn.java - allows user to key in.

import java.io.*;

publicabstractclass EasyIn

{

publicstatic String getString()

{

boolean ok =false;

String s =null;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

s = s.trim();

ok =true;

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return s;

}

publicstaticint getInt()

{

int i = 0;

boolean ok =false;

String s ;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

i = Integer.parseInt(s.trim());

ok =true;

}

catch(NumberFormatException e)

{

System.out.println("Make sure you enter an integer");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return i;

}

publicstaticbyte getByte()

{

byte i = 0;

boolean ok =false;

String s ;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

i = Byte.parseByte(s.trim());

ok =true;

}

catch(NumberFormatException e)

{

System.out.println("Make sure you enter a byte");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return i;

}

publicstaticshort getShort()

{

short i = 0;

boolean ok =false;

String s ;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

i = Short.parseShort(s.trim());

ok =true;

}

catch(NumberFormatException e)

{

System.out.println("Make sure you enter a short integer");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return i;

}

publicstaticlong getLong()

{

long l = 0;

boolean ok =false;

String s ;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

l = Long.parseLong(s.trim());

ok =true;

}

catch(NumberFormatException e)

{

System.out.println("Make surre you enter a long integer");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return l;

}

publicstaticdouble getDouble()

{

double d = 0;

boolean ok =false;

String s ;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

d = Double.parseDouble(s.trim());

ok =true;

}

catch(NumberFormatException e)

{

System.out.println("Make sure you enter a decimal number");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return d;

}

publicstaticfloat getFloat()

{

float f = 0;

boolean ok =false;

String s;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

f = Float.parseFloat(s.trim());

ok =true;

}

catch(NumberFormatException e)

{

System.out.println("Make sure you enter a decimal number");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return f;

}

publicstaticchar getChar()

{

char c =' ';

boolean ok =false;

String s;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

s =new String(b);

if(s.trim().length()!=1)

{

System.out.println("Make sure you enter a single character");

}

else

{

c = s.trim().charAt(0);

ok =true;

}

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

return c;

}

publicstaticvoid pause()

{

boolean ok =false;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.in.read(b);

ok =true;

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

}

publicstaticvoid pause(String messageIn)

{

boolean ok =false;

while(!ok)

{

byte[] b =newbyte[512];

try

{

System.out.print(messageIn);

System.in.read(b);

ok =true;

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

}

}

Finally, there are two errors in "TestIntegerStackWithExceptions.java"

publicclass TestIntegerStackWithExceptions

{

publicstaticvoid main(String[] args)

{

int size;

IntegerStackWithExceptions stack;

try

{

char choice;

// ask user to fix maximum size of stack

System.out.print("Maximum number of items on stack? ");

size = EasyIn.getInt();

// create new stack

stack =new IntegerStackWithExceptions(size);

// offer menu

do

{

System.out.println();

System.out.println("1. Push number onto stack");

System.out.println("2. Pop number from stack");

System.out.println("3. Check if stack is empty");

System.out.println("4. Check if stack is full");

System.out.println("5. Display numbers in stack");

System.out.println("6. Get any item from the stack");

System.out.println("7. Quit");

System.out.println();

System.out.print("Enter choice [1 - 7]: ");

choice = EasyIn.getChar();

System.out.println();

// process choice

switch(choice)

{// call static methods

case'1': option1(stack);break;

case'2': option2(stack);break;

case'3': option3(stack);break;

case'4': option4(stack);break;

case'5': option5(stack);break;

case'6': option6(stack);break;

case'7':break;

default: System.out.println("Invalid entry");

}

}

while (choice !='7');

}

catch (NegativeSizeException e)// from stack constructor call

{// source and type of error displayed

while (size != stack.getTotal())

{

System.out.println(e.getMessage());

System.out.println("due to error created by stack constructor");

System.out.print("Maximum number of items on stack? ");

size = EasyIn.getInt();

}

}

catch (InvalidStackPositionException e)// from 'option6' method call

{// source and type of error displayed

System.out.println(e.getMessage());

System.out.println("due to error created by call to 6th menu option");

}

catch (Exception e)// just in case any other exception is thrown

{

System.out.println("This exception was not considered");

System.out.println(e.getMessage());

}

EasyIn.pause("\nPress <Enter> to quit program");

}

// static method to push item onto stack

privatestaticvoid option1(IntegerStackWithExceptions stackIn)

{

System.out.print("Enter number: ");

int num = EasyIn.getInt();

boolean ok = stackIn.push(num);// attempt to push

if (!ok)// check if push was unsuccessful

{

System.out.println("Push unsuccessful");

}

}

// static method to pop item onto stack

privatestaticvoid option2(IntegerStackWithExceptions stackIn)

{

boolean ok = stackIn.pop();// attempt to pop

if (!ok)// check if pop was unsuccessful

{

System.out.println("Pop unsuccessful");

}

}

// static method to check if stack is empty

privatestaticvoid option3(IntegerStackWithExceptions stackIn)

{

if (stackIn.isEmpty())

{

System.out.println("Stack empty");

}

else

{

System.out.println("Stack not empty");

}

}

// static method to check if stack is full

privatestaticvoid option4(IntegerStackWithExceptions stackIn)

{

if (stackIn.isFull())

{

System.out.println("Stack full");

}

else

{

System.out.println("Stack not full");

}

}

// static method to display stack

privatestaticvoid option5(IntegerStackWithExceptions stackIn)throws InvalidStackPositionException

{

if (stackIn.isEmpty())

{

System.out.println("Stack empty");

}

else

{

System.out.println("Numbers in stack are: ");

System.out.println();

for (int i = 1; i <= stackIn.getTotal(); i++)

{

System.out.println(stackIn.getItem(i));

}

System.out.println();

}

}

// this method throws out any InvalidStackPositionException

privatestaticvoid option6(IntegerStackWithExceptions stackIn)

{

System.out.print("\nWhich position would you like to get? ");

int position = EasyIn.getInt();

try

{

System.out.println("This item is: " + stackIn.getItem(position));

}

catch (InvalidStackPositionException ispe)

{

while (position != stackIn.getItem(position))throws InvalidStackPositionException

{

System.out.println("Invalid position!");

System.out.print("\nWhich position would you like to get? ");

position = EasyIn.getInt();

}

}

System.out.println();

}

}

TestIntegerStackWithExceptions.java: Illegal start of expression at line 146

TestIntegerStackWithExceptions.java:';' expected at line 152

Hope to reply soon.

[31882 byte] By [edwinlcy153029] at [2007-9-30 20:21:01]
# 1

Next time, please don't paste all your code unless it's relevant to the problem. The compiler is telling you exactly where the problem is (most of the time, anyway), so look at the line it indicates to see if there is anything strange there or in the line above it. This type of error means you've violated the rules of Java syntax.

In this case you are trying to add a throws clause to a while loop, which is not allowed. You can only add throws clauses to methods (and constructors).

Herko_ter_Horst at 2007-7-7 1:05:51 > top of Java-index,Administration Tools,Sun Connection...