1

i got an error problem! I open my file i read a line and then i take information from the line with 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)

i wrote instead of last reader.readLine(), line=null and it worked

Code is ok its a StringTokenizer problem

examble of my txt format: 0 insert 370 D 425

1 Answer 1

1

The problem could be, you are looking for tab "\t" on you're stringTokenizer and maybe the space between youre data is not a tab is just a white space, try better line.split("\s+")

Sign up to request clarification or add additional context in comments.

2 Comments

no i said that it worked with one line i add a String "END" at txt if(line.startsWith("END")break; and its ok
We need youre orignal file to know what is really inside the file. btw oracle recommed the use of split than stringtokenizer "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.