mercredi 4 novembre 2015

Array Index out of bounds for Java using terminal

I am having trouble with a ticketing system that I am trying to create using java. I am trying to utilize two files to read and write my inputs. I am testing with just salesSell method at the moment. I am passing the args using terminal. The problem that I am having is an arrayoutofbounds exception that gets thrown. the two array parameters that I am passing are parsed from the same line in my current events file. an example of a line in the file is : "testevent1__________0003". I cannot seem to figure out where I am having this problem. Any type of guidance would be much appreciated.

package cisc_327_frontend;
import java.io.*;
import java.util.*;

public class Frontend_try1 {

private static File fileOutput;
private static List<StringBuilder> eventTrans = new ArrayList<>();


public static void consoleInput(String[] namesArray, int[] ticketArray,File fileCurrentEvents){
    System.out.println("Enter command:");
    Scanner input = new Scanner(System.in);
    String inputString = input.nextLine();


    boolean correct = true;

        do{

            if(inputString.toUpperCase().equals( "LOGIN")) {
                //enter login mode
                login(namesArray, ticketArray,fileCurrentEvents);
                correct = true;

                }
            if(inputString.toUpperCase().equals("LOGOUT")){
                logout(namesArray, ticketArray,fileCurrentEvents);
                correct=true;
            }
                if (!"LOGIN".toUpperCase().equals(inputString) || !"LOGOUT".toUpperCase().equals(inputString)){
                    System.out.println("Incorrect command, please enter command:");
                    input = new Scanner(System.in);
                    inputString = input.nextLine();
                    correct = false;
                }

        }while(!correct);



    }

public static void login(String[] namesArray, int[] ticketArray,File fileCurrentEvents) {
    System.out.println("Sales or Admin?");
    Scanner input = new Scanner(System.in);
    String inputString = input.nextLine();


    boolean correct = true ;

    do {
        if(inputString.toUpperCase().equals("LOGOUT")){
            logout(null, null, null);

        }
        else if(inputString.toUpperCase().equals("SALES")) {
            //enter sales mode
            sales(namesArray, ticketArray,fileCurrentEvents);
            correct = true;
        } else if (inputString.toUpperCase().equals("ADMIN")) {
            //enter admin mode
            admin(namesArray, ticketArray,fileCurrentEvents);
            correct = true;
        } else if (inputString.toUpperCase().equals("LOGOUT")) {
            //enter logout mode
            logout( namesArray, ticketArray, fileCurrentEvents);
            correct = true;
        } else  {
            //ask again
            System.out.println("Invalid Input");
            System.out.println("Sales or Admin?");
            input = new Scanner(System.in);
            inputString = input.nextLine();
            correct = false;
        }

    }while(!correct);

}

public static void sales(String[] namesArray, int[] ticketArray,File fileCurrentEvents) {
    //System.out.println("SALES");
    System.out.println("Sales Mode");
    System.out.println("Enter Command:");
    Scanner input = new Scanner(System.in);
    String inputString = input.nextLine();


    boolean correct = true ;

    do {
        if(inputString.toUpperCase().equals("LOGOUT")){
            logout( namesArray, ticketArray, fileCurrentEvents);

        }
        else if(inputString.toUpperCase().equals("SELL")) {
            //enter sales Sell mode
            salesSell(namesArray, ticketArray,fileCurrentEvents);
            correct = true;
        } else if (inputString.toUpperCase().equals("RETURN")) {
            //enter sales Return mode
            salesReturn(namesArray, ticketArray,fileCurrentEvents);
            correct = true;
        } else if (inputString.toUpperCase().equals("LOGOUT")) {
            //enter Logout mode
            logout( namesArray, ticketArray, fileCurrentEvents);
            correct = true;
        } else {
            //ask again
            System.out.println("Invalid Input");
            System.out.println("Enter Command:");
            input = new Scanner(System.in);
            inputString = input.nextLine();
            correct = false;
        }

    }while(!correct);



}


public static void salesSell(String[] namesArray, int[] ticketArray,File fileCurrentEvents) {
    int index = 0;
    String eventName = null;
    int numberTickets = 0;
    System.out.println("Sales Sell Mode");

    System.out.println("What is the name of your event?");
    Scanner input = new Scanner(System.in);
    String inputString = input.nextLine();

    if(inputString.toUpperCase().equals("LOGOUT")){
        logout( namesArray, ticketArray, fileCurrentEvents);

    }

    try{

        for(int i = 0; i < namesArray.length; i++) {
            if(namesArray[i].equals(inputString)){
                index = i;
            }
        }
         eventName= namesArray[index];
          numberTickets=ticketArray[index] ;

    } catch (Exception e) {
        System.out.println("Error: Event not found within file");
        System.exit(1);
    }

    int event = inputString.length();
    boolean charnumber = true;

    do{
        if(inputString.toUpperCase().equals("LOGOUT")){
            logout( namesArray, ticketArray, fileCurrentEvents);

        }
    if(event< 0 || event >20){

        System.out.println("No more than 20 characters allowed for name!");
        System.out.println("Enter name:");
        input = new Scanner(System.in);
        inputString = input.nextLine();
        event=inputString.length();
        charnumber = false;
    }
    else{
        charnumber = true;

    }

    } while(!charnumber);


    System.out.println("How many tickets?");
    input = new Scanner(System.in);
    inputString = input.nextLine();
    int digit;


    while(true){
    try {
        digit = Integer.parseInt(inputString);

    break;
    }
    catch(NumberFormatException e){


    }


    System.out.println("Please type a number!");    
    inputString = input.nextLine(); 
    }

if( numberTickets - digit <0){
    System.out.println("Illegal amount of tickets! Buy less ticket please.");

}
else{
    int tickets = numberTickets - digit;

    ticketArray[index]=tickets;


}
    boolean dignumber = true;

    do{
        if(inputString.toUpperCase().equals("LOGOUT")){
            logout( namesArray, ticketArray, fileCurrentEvents);

        }
    if(digit<0 || digit>8){

        System.out.println("Only 8 tickets allowed to be sold!");
        inputString = input.nextLine();
        event=inputString.length();

        dignumber = false;
        digit= Integer.parseInt(inputString);
    }
    else{
        dignumber= true;

    }
    }while(!dignumber);

}




public static boolean logout(String[] namesArray, int[] ticketArray,File fileCurrentEvents) {
    FileWriter logoutFileWriter;
    FileWriter currEventsFileWriter;
    int numSpaces = 0;

    try{
        currEventsFileWriter = new FileWriter(fileCurrentEvents, true);

            for(int i = 0; i < namesArray.length; i++ ) {
                currEventsFileWriter.write(namesArray[i]);
                numSpaces = 20 - (namesArray[i].length() + 4);
                for(int j = 0; j < numSpaces; j++) {
                    currEventsFileWriter.write("_");
                }
                currEventsFileWriter.write(ticketArray[i]);
                currEventsFileWriter.write(String.format("%n"));
            }       

    } catch(IOException e) {
        System.out.println("Rewriting Current Events File Error");
        System.exit(1);
    }

    try {
        logoutFileWriter = new FileWriter(fileOutput, true);

        //Cycle through event trans file and write via filewriter
        for(int i = 0; i < eventTrans.size(); i++ ) {
            String transact = eventTrans.get(i).toString();
            logoutFileWriter.write(transact);
            logoutFileWriter.write(String.format("%n"));
        }
        // Signify end of file
        logoutFileWriter.write("00                       000000 00000");
        logoutFileWriter.write(String.format("%n"));
        logoutFileWriter.close();
        fileOutput.createNewFile();

    } catch(IOException e) {
        System.out.println("Output File Error");
        System.exit(1);
    }
    return true;

    /*
    for( int i = 0; i < 20; i++ ) {
        System.out.println("");
    }
    System.out.println("Logged Out");
    consoleInput();
*/
}



public static void main(String[] args){

    try {
        File fileCurrentEvents = new File(args[0]);
        fileOutput = new File(args[1]);

        // Read current events file for events & dates
        FileReader fileRead = new FileReader(fileCurrentEvents);
        BufferedReader buffRead = new BufferedReader(fileRead);

        // Create strings for data in current events file
        String currentLine;
        String eventName;
        Integer numberTickets;
        List<Integer> numticket = new ArrayList<Integer>();
        List<String> list = new ArrayList<String>();
        int[] numarray= new int[numticket.size()];
        String[] linesArray = list.toArray(new String[list.size()]);
        // Cycle through current events file line by line
        while((currentLine = buffRead.readLine()) != null) {
            if (currentLine.equals("END_________________00000")){
                break; // End of file
            }

            //Parse for event name & number of tickets
            eventName = currentLine.substring(0, currentLine.lastIndexOf("_")).trim();
            numberTickets = Integer.parseInt(currentLine.substring(currentLine.lastIndexOf
                    ("_") + 1));



            // Place event name and # tickets into data structure
            // Add event name to event array list with same index as # tickets
            // Parse int to get # tickets & add to arraylist for tickets with same index


             while((eventName = buffRead.readLine()) != null){
                   list.add(eventName);
                }


                for (int i =0 ; i< list.size();i++){

                    linesArray[i]= list.get(i);

                }




                 while((eventName = buffRead.readLine()) != null){
                       numticket.add(numberTickets);
                    }


                    for (int i =0 ; i< numticket.size();i++){

                        numarray[i]= numticket.get(i);

                    }

        }

        while(true) {
            consoleInput(linesArray, numarray, fileCurrentEvents);
        }
    } catch (IOException e) {
        System.out.println("File I/O Error"); //Print to console to signify error
        e.printStackTrace();
        System.exit(1);
    }



}

}

Aucun commentaire:

Enregistrer un commentaire