import java.util.*; import java.io.*; public class counter extends Vector { static int i; //for initialization //constructors public counter( int a, int b ) { super( a, b ); } public counter( int a ) { super( a ); } //method to handle the main menu. private static void mainMenu() { //input streams for selections InputStreamReader into = new InputStreamReader( System.in ); BufferedReader buffTo = new BufferedReader( into ); System.out.println( "\nPlease enter a selection. 0 to exit, 1 to record a vector, or 2 to load a vector\n" ); //try statement for selection try { String s = buffTo.readLine(); if ( s.equals( "0" ) ) { System.exit( 1 ); } else if ( s.equals( "1" ) ) { createVector(); } else if (s.equals( "2" ) ) { loadVector(); } else { } } catch ( IOException e ) { System.out.println(e); } catch ( NumberFormatException e ) { System.out.println(e); } catch ( ArrayIndexOutOfBoundsException e ) { System.out.println(e); } } //simple print method private static void print( counter a ) { int b = a.size(); for ( i = 0; i < b; i++ ) { System.out.println( "\n" + ( i + 1 ) + ": " + a.get( i ) ); } } //overloading method that takes second argument, how far into the vector to print until it stops. commented out because it is currently not used. //private static void print( counter a, int printTil ) { // if ( printTil > a.size() ) { // print( a ); // } // else { // for ( i = 0; i < printTil; i++ ) { // System.out.println( ( i + 1 ) + ": " + a.get( i ) ); // } // } //} //method to create the vector private static void createVector() { //declarations InputStreamReader in = null; BufferedReader buff = null; counter vec; //try block try { //definition (initialization) vec = new counter( 0, 0 ); in = new InputStreamReader( System.in ); buff = new BufferedReader( in ); System.out.println( "Please enter a size for the vector\n" ); int size = Integer.parseInt( buff.readLine() ); vec.setSize( size ); //loop to let user know how many strings to input for ( i = 0; i < size; i++ ) { System.out.println( "Please enter " + ( size - i ) + " more strings" ); String s = buff.readLine(); vec.set( i, s ); } //pipe output to a txt file FileOutputStream exportTo = new FileOutputStream( "vector.txt" ); PrintStream exportOut = new PrintStream( exportTo ); for ( i = 0; i < size; i++ ) { exportOut.println( vec.get( i ) ); } System.out.println( "\nWould you like to print the vector? If so, type yes. To exit, type no. To go to the main menu, type menu. \n" ); String s = buff.readLine(); if ( s.equalsIgnoreCase( "yes" ) ) { print( vec ); } else if ( s.equalsIgnoreCase( "no" ) ) { System.exit( 1 ); } else if ( s.equalsIgnoreCase( "menu" ) ) { mainMenu(); } else { } } //end try //catch a few exceptions catch ( IOException e ) { System.out.println(e); } catch ( NumberFormatException e ) { System.out.println(e); } catch ( ArrayIndexOutOfBoundsException e ) { System.out.println(e); } } //method to load the vector private static void loadVector() { System.out.println( "Reading vector." ); counter v = new counter( 0, 0 ); String s = ""; //try to load the file try { FileReader read = new FileReader( "vector.txt" ); BufferedReader readTo = new BufferedReader(read); while ( s != null ) { s = readTo.readLine(); v.add( s ); } //remove the trailing null string v.remove( ( v.size() - 1 ) ); } catch ( IOException e ) { System.out.println( "\nVector loading failed " + e); } catch ( NumberFormatException e ) { System.out.println( "\nVector loading failed " + e ); } catch ( ArrayIndexOutOfBoundsException e ) { System.out.println( "\nVector loading failed " + e ); } System.out.println( "\nVector loaded." ); //loop to handle user input for selection while (true) { System.out.print( "\nType 0 to exit, 1 to print, and 2 to return to the menu.\n" ); InputStreamReader input = new InputStreamReader( System.in ); BufferedReader buffTo = new BufferedReader( input ); try { s = buffTo.readLine(); if ( s.equals( "1" ) ) { print( v ); } else if ( s.equals( "0" ) ) { System.exit( 1 ); } else if ( s.equals( "2" ) ) { mainMenu(); } else { } } catch ( FileNotFoundException e ) { System.out.println( "Vector loading failed " + e ); } catch ( IOException e ) { System.out.println( "Vector loading failed " + e); } catch ( NumberFormatException e ) { System.out.println( "Vector loading failed " + e ); } catch ( ArrayIndexOutOfBoundsException e ) { System.out.println( "Vector loading failed " + e ); } } } //main method public static void main( String[] args ) { mainMenu(); } }