简体   繁体   English

JAVA:阅读txt并使用StringTokenizer获取元素

[英]JAVA : Reading txt and take elements with StringTokenizer

i got an error problem! 我有一个错误问题! I open my file i read a line and then i take information from the line with StringTokenizer 我打开文件,读了一行,然后用StringTokenizer从行中获取信息

my code works with one line but when i am trying to read another i got an error any help ? 我的代码只能使用一行,但是当我尝试读取另一行时,我得到了任何帮助吗?

here is my code 这是我的代码

try{

    line = reader.readLine();

        while(line!=null){
            StringTokenizer st = new StringTokenizer(line,"\t");

             timer=st.nextToken("\t");
             int Itimer=Integer.parseInt(timer);
            // System.out.println(Itimer);
             what_to_do=st.nextToken("\t");
            // System.out.print(what_to_do);
             flightnumber=st.nextToken();
        int  Iflightnumber=Integer.parseInt(flightnumber);
            // System.out.print(Iflightnumber);
             departure=st.nextToken("\t");
            // System.out.print(departure);
             flighttime=st.nextToken("\t");

         int Iflighttime=Integer.parseInt(flighttime);
        // System.out.print(Iflighttime);
             Key=new KeyFlight(Iflightnumber,Iflighttime);
             flight=new Flight(Key,true);
             if(what_to_do.equals("insert")){
            // System.out.print("worked");
             if(departure.equals("D")){
                 result=true;
             }else{result=false;}
             flight.setdeparture(result);//8a mporousa na kanw new flight alla gia e3ikonomisi to ekana me seter//
             EV.insert(flight);
            // System.out.println("worked again");


             }else if(what_to_do.equals("cancel")){
                 EV.remove(Key);
                                 }
             else if(what_to_do.equals("update")){
                 EV.UpdateKey(flight, Key);
             }
            line=reader.readLine();

and these are the errors Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at java.util.StringTokenizer.nextToken(Unknown Source) at FlightSchedule.loadandStoreFile(FlightSchedule.java:54) at FlightSchedule.main(FlightSchedule.java:13) 这些是错误“ FairSchedule.loadandStoreFile(FlightSchedule.java:54)处的” main“线程” java.util.NoSuchElementException“中的异常。 )在FlightSchedule.main(FlightSchedule.java:13)

i wrote instead of last reader.readLine(), line=null and it worked 我写了而不是最后一个reader.readLine(),line = null并且它起作用了

Code is ok its a StringTokenizer problem 代码可以解决StringTokenizer问题

examble of my txt format: 0 insert 370 D 425 我的txt格式的 序言 0插入370 D 425

问题可能是,您正在stringTokenizer上寻找选项卡“ \\ t”,也许您之间的空间不是选项卡只是空白,请尝试使用更好的line.split(“ \\ s +”)

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

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