0

Java で予約システムを作成しようとしていますが、プログラムを実行するたびに、while ループ (以下を参照) は、読み取られた行が 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 + "'");
        }
    }
}`

どんなアイデア/助けも大歓迎です

EDIT は、要求に応じて while ループに残りのコードを追加しました

ここで編集は、私が読んでいるファイルです

Film 1
10
10
5.00
10.30
Wednesday 23rd
July
2013
4

3 に答える 3

3

これが問題に関連しているかどうかはわかりませんが、 のbreak;各コード シーケンスの後にステートメントを配置する必要がありますswitch。それ以外の場合、sayitem_counterが 0 の場合、0 のコードを実行し、フォールスルーして 1 のコードを実行し、次に 2 のコードを実行します。

于 2013-08-19T16:25:29.040 に答える
0

これを試して...

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

                }
于 2013-08-19T16:36:26.737 に答える