This bug is killing me. I have no idea what I did wrong. My friend has it the exact same way. This program reads a txt file and reads each line which has a request in it EX:
Matt Smith;Event 1;2009;March;29;11;120;40;
We take the request cut it up according to the ; and put the name, event, date in an instance of request object. However when i try using the string which i cut up. compiler complains ArrayIndexOutOfBoundsException on my array index.
Main Method
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
public class Reservation
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
Room room1 = new Room(50);
Room room2 = new Room(100);
String[] months = {"January", "Febuary", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
while(true)
{
System.out.println("Enter the filename that contains input.txt");
Scanner in = new Scanner(System.in);
String filename = in.next();
Scanner input = new Scanner(new FileReader(filename));
while (input.hasNextLine())
{
String line = input.nextLine();
String[] str = line.split(";");
int monthNum = 0;
for (int i = 0; i != 12; i++)
{
if ((str[3]).equals(months[i]))
{
monthNum = i; break;
}
}
GregorianCalendar date = new GregorianCalendar(Integer.parseInt(str[2]), monthNum, Integer.parseInt(str[4]), Integer.parseInt(str[5]), 0);
Request request = new Request(str[0], str[1], date, Integer.parseInt(str[6]), Integer.parseInt(str[7]));
if (request.getDate().get(GregorianCalendar.HOUR_OF_DAY) > 22 && 9 < request.getDate().get(GregorianCalendar.HOUR_OF_DAY))
{
System.out.println("Hour of Day in Request is not within office hours 9am-10pm");
System.out.println("Please try another request");
continue;
}
if (request.getSeats() <= 50)
{
if (room1.add(request)){}
else {room2.add(request);}
}
else {room2.add(request);}
}
break;
}
}
}
It complains the in the first instance were I use str[3].
Request should have nothing to do with the bug but here it is.
Request(String name, String event, GregorianCalendar date, int duration, int seats)
{
this.name = name;
this.event = event;
this.date = date;
this.duration = duration;
this.seats = seats;
}