简体   繁体   English

java文件readline返回null

[英]java file readline returns null

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 我试图用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 + "'");
        }
    }
}`

any ideas / help would be greatly appreciated 任何想法/帮助将不胜感激

EDIT added rest of the code in the while loop as requested EDIT根据请求在while循环中添加了其余代码

EDIT here is the file I am reading from 编辑这里是我正在读取的文件

Film 1
10
10
5.00
10.30
Wednesday 23rd
July
2013

I don't know if this is related to the problem, but you need to put break; 我不知道这是否与问题有关,但是您需要break; statements after every code sequence in your switch . 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. 否则,如果说item_counter为0,它将执行0的代码,然后掉落并执行1的代码,然后执行2的代码, item_counter

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...

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

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM