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;
    }

Recommended Answers

All 6 Replies

Member Avatar for ztini

Check your .txt file, there may be a line that does not contain enough ";" which would then create an array of the wrong size when you do line.split(";").

You should really work on breaking your code apart in methods. In this case, something like this:

import java.io.BufferedReader;

public class Reservation {

	public Reservation() {
		String fileName = getInput();
		BufferedReader reader = openFile(fileName);
		parseFile(reader);
		closeFile(reader);
	}
	
	private String getInput() { 
		return null; 
	}
	private BufferedReader openFile(String fileName) { 
		return null;
	}
	private void parseFile(BufferedReader reader) { }
	private void closeFile(BufferedReader reader) { }
	private static void main(String[] args) {
		new Reservation();
	}
	
}

Can't diagnose it visually - ztini may be right, a problem with your input (is there a leading line without semicolons? That would do it) - but here's two suggestions for diagnosis.

1) you're doing this in a loop - two loops, actually. To pinpoint the error more precisely, put in some println statements, at the start of the while(hasNextLine) loop and the start of the for loop, with some sentinel data (i, in the for loop, maybe print out the line that's read in the while loop). That way you know where the problem is coming up, and something about what the machine is chewing on there.
2) you have two array references on that line. break them up onto separate lines to see which one is giving you trouble. I'm pretty sure it's your input, in this case, but "pretty sure" is not a good debugging technique.

Also - your loop condition on that for loop is not standard idiom. It works, but it looks fragile to me. Stick with <= if you're looping while <=. If nothing else, it'll save your colleagues time squinting at your weird loop condition, trying to make sure it's right when you ask them to help you find bugs.

Thanks. That was a stupid bug.
I would use different methods but my professor doesn't allow us such liberties.

So what was the bug?

Member Avatar for ztini

Your professor doesn't allow you to use methods? That's the whole point of OOP...and really programming in general -- to break apart a problem into smaller tasks.

Either way, make sure you close your thread.

The bug was that I had empty lines in my input.txt and i was missing a field in one of the requests of input.txt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.