0

I am trying to create a booking system in Java, however every time I run the program the while loop (shown below) skips straight to the end as though the line read was null

   //hardcoded file path - needs to be changed when program moved
            String fileName = "C:\\Users\\BOO\\Desktop\\SystemsSoftwareBookingsystem\\FilmData.txt";
            String line = null;
            int readInt = 0;
            float readFloat = 0;
            int item_counter = 0;

    try
            {

             BufferedReader bufferedReaderF = new BufferedReader(new FileReader(new File(fileName)));  

 while ((line = bufferedReaderF.readLine()) != null)
            {
                Film tmpFilm = new Film();

                switch (item_counter)
                {
                    case 0:
                {
                    line = bufferedReaderF.readLine();
                    tmpFilm.name = line;
                    item_counter++;
                    break;
                }
                case 1:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.seatsTotal = readInt;
                    item_counter++;
                    break;
                }
                case 2:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.seatsAvailable = readInt;
                    item_counter++;
                    break;
                }
                case 3:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.price = readFloat;
                    item_counter++;
                    break;
                }
                case 4:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.showingTime = readFloat;
                    item_counter++;
                    break;
                }
                case 5:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.day = readInt;
                    item_counter++;
                    break;
                }
                case 6:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.month = readInt;
                    item_counter++;
                    break;
                }
                case 7:
                {
                    readInt = bufferedReaderF.read();
                    tmpFilm.year = readInt;
                    item_counter = 0;
                    break;
                }
                }

                line = bufferedReaderF.readLine();

                server.filmList.add(tmpFilm);

            }

            bufferedReaderF.close();

        } catch (FileNotFoundException ex)
        {
            System.out.println("Unable to open file '" + fileName + "'");
        } catch (IOException ex)
        {
            System.out.println("Error reading file '" + fileName + "'");
        }
    }
}`

any ideas / help would be greatly appreciated

EDIT added rest of the code in the while loop as requested

EDIT here is the file I am reading from

Film 1
10
10
5.00
10.30
Wednesday 23rd
July
2013
1
  • Are you sure the file isn't empty? Commented Aug 19, 2013 at 16:10

3 Answers 3

3

I don't know if this is related to the problem, but you need to put break; statements after every code sequence in your switch. Otherwise, if say item_counter is 0, it will execute the code for 0, and then fall through and execute the code for 1, and then for 2, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for pointing that out - i've fixed that now but it wasn't what was causing the problem
0

Remove-

line = bufferedReaderF.readLine();

And read in the loop-

while ((line = bufferedReaderF.readLine()) != null)

Check if the file is empty. Can you also update your question with code inside while?

Based from your edit-

You are ignoring what you are reading in while. Should be something like this-

switch (item_counter)
{
    case 0:
    {
        tmpFilm.name = line;
        item_counter++;
        break;
    }
    case 1:
    {                    
        tmpFilm.seatsTotal = Integer.parseInt(line);
        item_counter++;
        break;
    }
    ...etc...

5 Comments

That bottom line does not compile, it needs to be while((line = bufferedReaderF.readLine()) != null){ ..... }.
@JimGarrison - His variable name is "bufferedReaderF".
I reverted the edit already; however, this answer does not appear to address the actual problem. The edit suggested, if not coupled with removing the additional read at the end of the loop will cause alternate input lines to be skipped.
i have tried making these changes but unfortunately it still won't work for some reason
@JimGarrison - The answer was given before the edit in question was made...also the edit in question was made because of what I requested in answer..
0

Try this...

BufferedReader bufferedReaderF = new BufferedReader(new FileReader(new File(fileName)));  
line = bufferedReaderF.readLine();
 while (line != null)
                {
                    Film tmpFilm = new Film();

                    switch (item_counter)
                    {
                        case 0:
                        {
                           // line = bufferedReaderF.readLine();
                            tmpFilm.name = line;
                            item_counter++;
                            break;
                        }
                        case 1:
                        {
                            readInt=Integer.parseInt(line);
                            tmpFilm.seatsTotal = readInt;
                            item_counter++;
                            break;
                        }
                        case 2:
                        {
                            readInt = Integer.parseInt(line);
                            tmpFilm.seatsAvailable = readInt;
                            item_counter++;
                            break;
                        }
                        case 3:
                        {
                            readFloat=Float.parseFloat(line);
                            tmpFilm.price = readFloat;
                            item_counter++;
                            break;
                        }
                        case 4:
                        {
                            readFloat=Float.parseFloat(line);
                            tmpFilm.showingTime = readFloat;
                            item_counter++;
                            break;
                        }
                        case 5:
                        {
                            line=line.replaceAll("\\D","");
                            readInt = Integer.parseInt(line);
                            tmpFilm.day = readInt;
                            item_counter++;
                            break;
                        }
                        case 6:
                        {
                            readInt = Integer.parseInt(GregorianCalendar.class.getField(line.toUpperCase()).get(line))+1;
                            tmpFilm.month = readInt;
                            item_counter++;
                            break;
                        }
                        case 7:
                        {
                            readInt = Integer.parseInt(line);
                            tmpFilm.year = readInt;
                            item_counter = 0;                   
                        }
                    }

                    line = bufferedReaderF.readLine();

                    server.filmList.add(tmpFilm);

                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.