没有发现线?读取文件错误?

时间:2015-04-26 02:42:40

标签: java file-io while-loop

我在循环时读取此No Line found错误。 即使用input.nextLine()打印所有内容,它也会显示countt == 0。 这意味着,虽然循环甚至没有工作?

public class ReaderFile {
    public static Scanner input;

尝试捕获输入。

try{
            input = new Scanner(new File(fileLocation)).useDelimiter(",");
        }
        catch (IOException ioException)
        {
            System.out.print("PROBLEM");
        }

这是我遇到问题的代码。

int countt = 0;
        input.nextLine();
        while(input.hasNext())
               {
                      System.out.print("TEAMSTart\n");
                      int ID = input.nextInt();
                      String teamName = input.next();
                      String coachFirst = input.next();
                      String coachLast = input.next();
                      String mentorFirst = input.next();
                      String mentorLast = input.next();
                      String teamFs = input.next();
                      String teamSS = input.next();
                      input.nextLine();
                      Team team = new Team (teamName, ID, coachFirst, coachLast,mentorFirst,mentorLast,teamFs,teamSS);
                      store.add(team);
                      System.out.print(ID);
                      countt = countt+1;
                      //System.out.print("\n"+countt);
                }

这是我正在阅读的文本文件

TeamNumber,Team Name,Coach First,Coach Last,Mentor First,Mentor Last,Team Fin Sponsor,Schools or Sponsoring Organization,TmMem1First,TmMem1Last,TmMem2First,TmMem2Last,TmMem3First,TmMem3Last,TmMem4First,TmMem4Last,TmMem5First,TmMem5Last,TmMem6First,TmMem6Last,TmMem7First,TmMem7Last,TmMem8First,TmMem8Last
6842,Reagan Ray-Guns,Judy,Mallon,Aziz,Valdez,Texas Workforce Commission,REAGAN H S,Steven,Cepeda,Alan,Yue,Tim,Callaway,Damon,Bertucci,Samuel,de Olvieira,Samuel,Day,,,,
6888,Islanders,Judy,Maldonado,Brady,Trevino,Three Rivers Robotics,THREE RIVERS MIDDLE,Shireen,Cowdrey,Dee,Roundtree,Steven,Callaway,Francisco,Bermea,,,,,,,,
7004,GREENHILL Tops,Kanat,LaBass,Harvey,Pflueger,GREENHILL Boosters,GREENHILL SCHOOL,Harvey,Pflueger,Sandra,Day,Denny,Rodriguez,shirley,Couvillon,Carly,Szarka,,,,,,

2 个答案:

答案 0 :(得分:0)

首先,我建议您使用try-with-resources Statement当您获得Exception时,不要只说问题

try (Scanner input = new Scanner(new File(fileLocation))) {

} catch (IOException ioException) {
    ioException.printStackTrace();
}

我会逐行阅读。请记住,您应该在尝试阅读之前致电hasNext,然后可以在,上分享

while (input.hasNextLine()) {
    int col = 0;
    String line = input.nextLine();
    String[] arr = line.split(",");
    System.out.print("TEAMSTart\n");
    int ID = Integer.parseInt(arr[col++]);
    String teamName = arr[col++];
    String coachFirst = arr[col++];
    String coachLast = arr[col++];
    String mentorFirst = arr[col++];
    String mentorLast = arr[col++];
    String teamFs = arr[col++];
    String teamSS = arr[col];
    store.add(new Team(teamName, ID, coachFirst, coachLast,
            mentorFirst, mentorLast, teamFs, teamSS));
    System.out.println(ID);
}

您的文件似乎以标题开头...您可以添加

if (input.hasNextLine()) {
    String hdr = input.nextLine();
}

在上面的while循环之前跳过它。

答案 1 :(得分:0)

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Reader {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = null;
        try {
            input = new Scanner(new File("C:/Mohan_Backup/DDR/input.txt"))
                    .useDelimiter(",");
        } catch (IOException ioException) {
            System.out.print("PROBLEM");
        }

        int countt = 0;
        System.out.println(input.nextLine());
        while (input.hasNext()) {
            System.out.print("\nTEAMSTart: ");
            int ID = input.nextInt();
            String teamName = input.next();
            String coachFirst = input.next();
            String coachLast = input.next();
            String mentorFirst = input.next();
            String mentorLast = input.next();
            String teamFs = input.next();
            String teamSS = input.next();
            input.nextLine();
            // Team team = new Team (teamName, ID, coachFirst,
            // coachLast,mentorFirst,mentorLast,teamFs,teamSS);
            // store.add(team);
            System.out.print(ID);
            countt = countt + 1;
            // System.out.print("\n"+countt);
        }
    }

}

This code works  and shows the below output
TeamNumber,Team Name,Coach First,Coach Last,Mentor First,Mentor Last,Team Fin Sponsor,Schools or Sponsoring Organization,TmMem1First,TmMem1Last,TmMem2First,TmMem2Last,TmMem3First,TmMem3Last,TmMem4First,TmMem4Last,TmMem5First,TmMem5Last,TmMem6First,TmMem6Last,TmMem7First,TmMem7Last,TmMem8First,TmMem8Last

TEAMSTart: 6842
TEAMSTart: 6888
TEAMSTart: 7004

我评论了第35,36,37行,因为我没有相同的来源。