//TestListMenu
//Nick Stoner Oct 31, 2008
import list.*;
import java.io.*;
//Tests list by user input via menu options
//
public class TestListMenu
{
public static void main(String[]args)throws IOException
{
List theList=new LinkedList();
while(true)
{
printMenu();
System.out.println("Execute which option?");
int choice=userInput();
if(choice==9)
break;
processChoice(choice,theList);
System.out.println("Current list: "+theList.toString());
}
}
//post: returns users menu choice
public static int userInput()throws IOException
{
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader keyReader=new BufferedReader(reader);
String line=null;
int choice=0;
while(choice==0)
{
try
{
line=keyReader.readLine();
if(line.equals(""))
break;
choice=new Integer(line);
}
catch(NumberFormatException ex)
{
System.out.println(line+" is not a valid menu choice.");
}
}
return choice;
}
// post: prompts for and reads in an int to use for an Index
// in testing List methods, returns that int
public static int getIndex() throws IOException
{
InputStreamReader indexReader=new InputStreamReader(System.in);
BufferedReader indexKeyReader=new BufferedReader(indexReader);
String indexLine=null;
int indexChoice=0;
while(indexChoice==0)
{
indexLine=indexKeyReader.readLine();
if(indexLine.equals(""))
{
System.out.println("Please enter an integer");
}
indexChoice=new Integer(indexLine);
}
return indexChoice;
}
//post: prompts for and reads in an Integer to use for an Element
// in testing List methods, returns the Element
public static Integer getElement() throws IOException
{
InputStreamReader elementReader=new InputStreamReader(System.in);
BufferedReader elementKeyReader=new BufferedReader(elementReader);
String elementLine=null;
Integer elementChoice=new Integer(0);
while(elementChoice==0)
{
elementLine=elementKeyReader.readLine();
if(elementLine.equals(""))
{
System.out.println("Please enter an integer");
}
elementChoice=new Integer(elementLine);
}
return elementChoice;
}
//post: prints menu options
public static void printMenu()
{
System.out.println("Test Stack operations: "
+"\n1) add an integer to the list at index #_"
+"\n2) remove the integer at index #_ from the list"
+"\n3) find out if the list contains a number"
+"\n4) get size of the list"
+"\n5) tell whether the list is empty or not"
+"\n6) tell what is at index #_"
+"\n7) set a new integer at index #_"
+"\n8) tell what index # _ is at"
+"\n9) quit");
}
//post: do chosen operation on theStack and print results
public static void processChoice(int choice,List theList)
{
if(choice==1)//add
{
Integer element=getElement();
int index=getIndex();
theList.add(index,element);
}
else if(choice==2)//remove
{
int index=getIndex();
theList.remove(index);
}
else if(choice==3)//find
{
int index=getIndex();
theList.contains(index);
}
else if(choice==4)//size
{
System.out.println("The size is: "+theList.size());
}
else if(choice==5)//list empty or not
{
System.out.println("Is current list empty? "+theList.isEmpty());
}
else if(choice==6)//what is at index#
{
int index=getIndex();
theList.get(index);
}
else if(choice==7)//set new integer at index#
{
int index=getIndex();
Integer element=getElement();
theList.set(index,element);
}
else if(choice==8)//what is the index of #_
{
Integer element=getElement();
theList.indexOf(element);
}
}
}
Quote
----jGRASP exec: javac -g J:\cs350\TestListMenu.java
TestListMenu.java:114: unreported exception java.io.IOException; must be caught or declared to be thrown
Integer element=getElement();
^
TestListMenu.java:115: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:120: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:125: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:138: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:143: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:144: unreported exception java.io.IOException; must be caught or declared to be thrown
Integer element=getElement();
^
TestListMenu.java:149: unreported exception java.io.IOException; must be caught or declared to be thrown
Integer element=getElement();
^
8 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
TestListMenu.java:114: unreported exception java.io.IOException; must be caught or declared to be thrown
Integer element=getElement();
^
TestListMenu.java:115: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:120: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:125: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:138: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:143: unreported exception java.io.IOException; must be caught or declared to be thrown
int index=getIndex();
^
TestListMenu.java:144: unreported exception java.io.IOException; must be caught or declared to be thrown
Integer element=getElement();
^
TestListMenu.java:149: unreported exception java.io.IOException; must be caught or declared to be thrown
Integer element=getElement();
^
8 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Ok so those are the errors I'm getting after compiling in jGRASP. I'm not sure why I'm getting this message because the getElement() and getIndex() methods both declare to throw IOExceptions. What is wrong with my code?
Much thanks for any help!


Sign In
Create Account

Back to top









