0

我正在尝试用 Java 创建一个预订系统,但是每次我运行程序时,while 循环(如下所示)都会直接跳到最后,就好像读取的行为空一样

   //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 回答
0

消除-

line = bufferedReaderF.readLine();

并在循环中阅读-

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

检查文件是否为空。你也可以用里面的代码更新你的问题吗?

根据您的编辑-

您正在忽略您正在阅读的内容。应该是这样的——

switch (item_counter)
{
    case 0:
    {
        tmpFilm.name = line;
        item_counter++;
        break;
    }
    case 1:
    {                    
        tmpFilm.seatsTotal = Integer.parseInt(line);
        item_counter++;
        break;
    }
    ...etc...
于 2013-08-19T16:10:39.060 回答