1

I have file.txt:

7 10 5
ADD_FLIGHT SV221 Jeddah NewYork 30 7000
ADD_FLIGHT SV223 Jeddah London 30 4000
ADD_FLIGHT SV225 Jeddah Paris 30 3500
ADD_FLIGHT SV227 Jeddah Cairo 30 2000
ADD_PASS Mohammed Ali 33 M 0555788778
ADD_PASS Sara Maghrabi 30 F 0555111111
ADD_PASS Hani Ali 20 M 0555223344
ADD_PASS Mohammed Hafeth 33 M 0555889876
ADD_PASS Ahmad Sami 44 M 0555768768
ADD_FLIGHT SV332 Jeddah Riyadh 20 500
ADD_FLIGHT SV334 Jeddah Dammam 20 600
ADD_FLIGHT SV367 Jeddah Dubai 25 2000
ADD_PASS Salwa Ali 33 F 0555765672
ADD_PASS Faisal Amri 20 M 0555111111
ADD_PASS Mona Saleem 33 F 0555222112
ADD_PASS Ali Ali 33 M 0555743344
ADD_PASS Marwa Ahmad 33 F 0555545855

I want to read information flight from the file and put the information in an array of object if the file Contains ADD_flight statement .. Also the passengers read information passenger from file and put the information in an array of object if the file Contains ADD_PASD statement.

I don't know why I have error expiation in my code:

File fin = new File("input.txt");
Scanner input = new Scanner(fin);

int c=0;
while (input.hasNextLine()){
     String s=input.nextLine();
     if (input.hasNext("ADD_FLIGHT")){
        inputFlight ( input,  flight ,fin );  
     }
     else if (input.hasNext("ADD_PASS")){
         inputPass( input,  passenger,fin );
          listFlightDetails( flight);
          listPassengerDetails(passenger);
     }}}//end the mine 


public static void inputFlight (Scanner input, Flight[] flight ,File fin ) throws IOException{
if (indexFlight<flight.length)  {
   flight[indexFlight]=new Flight();
   String flightCode=input.next();
    flight[indexFlight].setflightCod(flightCode);
        String ctyfrom=input.next();
    flight[indexFlight].setcityFrom(ctyfrom);
        String ctyto=input.next();
    flight[indexFlight].setCityTo(ctyto);
             int total=input.nextInt();
                  flight[indexFlight].setTotalSeats(total);
             double price=input.nextDouble();
                  flight[indexFlight].setprice(total);

indexFlight++;

}}
     public static void inputPass( Scanner input,  Passenger[] passenger ,File fin ) throws IOException{
if (indexPassenger<passenger.length)  {
   passenger[indexPassenger]=new Passenger();
   String name=input.next();
    passenger[indexPassenger].setname(name);
        int age=input.nextInt();
    passenger[indexPassenger].setage(age);
        char gender=input.nextLine().charAt(0);
    passenger[indexPassenger].setgender(gender);
            String d=input.next();
                  passenger[indexPassenger].setphone(d);

indexPassenger++;

}}
      public static void listFlightDetails(Flight[] flight) {
       for (int i = 0; i < indexFlight; i++) {
           if (flight[i].getflightCod() != null) {
               System.out.println("Enter " + i + " for Flight code :" + flight[i].getflightCod() + " , " + flight[i].getcityFrom() + " , " + flight[i].getCityTo());


}}}
public static void listPassengerDetails(Passenger[] passenger) {
       for (int i = 0; i < indexPassenger; i++) {
           if (passenger[i].getname() != null) {
               System.out.println("Enter " + i + " for  Passenger  :" + passenger[i].getname() + " , " + passenger[i].getgender());
           }

 }

}

How can I correct the code? This error, which comes

Exception in thread "main" java.util.InputMismatchException
   at java.util.Scanner.throwFor(Scanner.java:864)
   at java.util.Scanner.next(Scanner.java:1485)
   at java.util.Scanner.nextInt(Scanner.java:2117)
   at java.util.Scanner.nextInt(Scanner.java:2076)
   at FlightSystem.FlightSystem.inputFlight(FlightSystem.java:65)
   at FlightSystem.FlightSystem.main(FlightSystem.java:34)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
This error for pass

    Enter 0 for Flight code :SV223 , Jeddah , London
        Enter 0 for Flight code :SV223 , Jeddah , London
        Enter 0 for Flight code :SV223 , Jeddah , London
        Enter 1 for Flight code :SV227 , Jeddah , Cairo
        Enter 0 for Flight code :SV223 , Jeddah , London
        Enter 1 for Flight code :SV227 , Jeddah , Cairo
        Exception in thread "main" java.util.InputMismatchException
           at java.util.Scanner.throwFor(Scanner.java:864)
           at java.util.Scanner.next(Scanner.java:1485)
           at java.util.Scanner.nextInt(Scanner.java:2117)
           at java.util.Scanner.nextInt(Scanner.java:2076)
           at FlightSystem.FlightSystem.main(FlightSystem.java:39)
        Java Result: 1
8
  • Did you mean, if a line starts with "ADD_FLIGHT" you want to insert the details in flight array and if it starts with "ADD_PASS" you want to store it in passenger array? Commented Apr 7, 2015 at 10:04
  • What error are you getting? Commented Apr 7, 2015 at 10:11
  • This error, which comes Commented Apr 7, 2015 at 10:27
  • Which line is line 65 in the FlightSystem class? Commented Apr 7, 2015 at 10:28
  • flight[indexFlight].setcityFrom(ctyfrom); //this line 65 Commented Apr 7, 2015 at 10:32

3 Answers 3

1

To keep it simple, can we try something like :

while (input.hasNextLine()) {
    String s=input.nextLine();
    if(s.startsWith("ADD_FLIGHT")) {
        // Add to list of Flight DTO
    }
    else if(s.startsWith("ADD_PASS")) {
        // Add to list of passanger DTO
    }

    s = null;
}

I doubt for first line of your text file :

flightCode is coming as : ADD_FLIGHT
ctyfrom coming as : SV223
ctyto is coming as : Jeddah
and total is coming as "NewYork" which can't be converted into int.

You can put Sysout to verify it, or put a debug point as well.

If my doubt is correct then add input.next(); just before line String flightCode=input.next(); in method inputFlight()

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

4 Comments

Can we see Flight class? can you check the type of "cityFrom" attribute
While this might be an improvement to the code in general, this does not address the problem itself.
This can work. We can split() the string with " " in the if loop and then add data to the object array. This is a lot more easier.
@LittlePanda I completely agree with with.
0

Your code

if (input.hasNext("ADD_FLIGHT")){
        inputFlight ( input,  flight ,fin );  
     }

Points to this line of your file ADD_FLIGHT SV221 Jeddah NewYork 30 7000.

Now in your inputFlight(...) method, read the comments carefully of below code. I explained why you got InputMisMatchException.

public static void inputFlight (Scanner input, Flight[] flight ,File fin ) throws IOException{
if (indexFlight<flight.length)  {
   flight[indexFlight]=new Flight();
   String flightCode=input.next();// This line takes ADD_FLIGHT instead of flight code SV221 
    flight[indexFlight].setflightCod(flightCode);
        String ctyfrom=input.next();//This line takes SV221 instead of Jeddah
    flight[indexFlight].setcityFrom(ctyfrom);
        String ctyto=input.next();//This line takes Jeddah instead of NewYork
    flight[indexFlight].setCityTo(ctyto);
             int total=input.nextInt();//This line takes NewYork instead of 30 thus InputMisMatchException occurs.
                  flight[indexFlight].setTotalSeats(total);
             double price=input.nextDouble();
                  flight[indexFlight].setprice(total);

indexFlight++;

}}

** int total=input.nextInt();//This line takes NewYork instead of 30 thus InputMisMatchException occurs. because your are trying to get Int but input gets a string from file.

To avoid the exception just add input.next(); before you reading other string. Here is the code.

public static void inputFlight (Scanner input, Flight[] flight ,File fin ) throws IOException{
if (indexFlight<flight.length)  {
   String not_in_use=input.next()//**for moving input cursor to next (flight code)**
   flight[indexFlight]=new Flight();
   String flightCode=input.next();
    flight[indexFlight].setflightCod(flightCode);
        String ctyfrom=input.next();
    flight[indexFlight].setcityFrom(ctyfrom);
        String ctyto=input.next();
    flight[indexFlight].setCityTo(ctyto);
             int total=input.nextInt();
                  flight[indexFlight].setTotalSeats(total);
             double price=input.nextDouble();
                  flight[indexFlight].setprice(total);

indexFlight++;

}}

For inputPass(...) method, indicating this line ADD_PASS Salwa Ali 33 F 0555765672 of your file. Read the comment of below code carefully.

public static void inputPass( Scanner input,  Passenger[] passenger ,File fin ) throws IOException{
    if (indexPassenger<passenger.length)  {
       passenger[indexPassenger]=new Passenger();
       String name=input.next();//Taking ADD_PASS instead of Salwa
        passenger[indexPassenger].setname(name);
            int age=input.nextInt();//Trying to take an integer but found string Salwa thus occurred InputMisMatchException. 
        passenger[indexPassenger].setage(age);
            char gender=input.nextLine().charAt(0);
        passenger[indexPassenger].setgender(gender);
                String d=input.next();
                      passenger[indexPassenger].setphone(d);

    indexPassenger++;

    }}

** int age=input.nextInt();//Trying to take an integer but found string Salwa thus occurred InputMisMatchException.

Try this,

public static void inputPass( Scanner input,  Passenger[] passenger ,File fin ) throws IOException{
    if (indexPassenger<passenger.length)  {
       String not_in_use=input.next();//avoiding ADD_PASS
       passenger[indexPassenger]=new Passenger();
       String first_name=input.next();//taking first name
       String last_name=input.next();//taking last name
        passenger[indexPassenger].setname(first_name+" "+last_name);
            int age=input.nextInt();//taking age
        passenger[indexPassenger].setage(age);
            String gender=input.next();//taking gender
        passenger[indexPassenger].setgender(gender.toCharArray()[0]);
                String d=input.next();//taking phone number
                      passenger[indexPassenger].setphone(d);

    indexPassenger++;

    }}

5 Comments

Welcome. I have edited my answer for your inputPass(...) method, but I didn't run this on my computer but I hope that this will work.
can you please explain what types of error you got on inputPass(...) method?
The code I added last for inputPass(...) method it is working fine in my computer. May be you have some other problem/issue.
your error shows that you have an error in line number 39. Which is your line number 39?
May be you have some other problem, because the code I added last time for inputPass(...) method it is working fine in my computer.
0

I have tried doing this with Regex. See below code:

List<String> filecontent = Files.readAllLines(Paths.get("abc.txt"), Charset.defaultCharset());

//regex for ADD_FLIGHT 
Pattern addFlight = Pattern.compile("ADD_FLIGHT (.+) (.+) (.+) (.+) (.+)");
//regex for ADD_PASS
Pattern addPass = Pattern.compile("ADD_PASS (.+) (.+) (.+) (.+) (.+)");

for(int i=0;i<filecontent.size();i++)
{
    Matcher m1 = addFlight.matcher(filecontent.get(i));
    while(m1.find())
    {
        //System.out.println(m1.group(0));
        //**Add each piece of data given in each line to your object array here**
        System.out.println(m1.group(1)); //SV221 
        System.out.println(m1.group(2)); //Jeddah 
        System.out.println(m1.group(3)); //NewYork 
        System.out.println(m1.group(4)); //30
        System.out.println(m1.group(5)); //7000
    }
    Matcher m2 = addPass.matcher(filecontent.get(i));
    while(m2.find())
    {
        //**Add each piece of data given in each line to your object array here**
        //System.out.println(m2.group(0)); //entire sentence
        System.out.println(m2.group(1)); //marwa
        System.out.println(m2.group(2)); //ahmad
        System.out.println(m2.group(3)); //33
        System.out.println(m2.group(4)); //F
        System.out.println(m2.group(5)); //0555545855
    }
}

Add each piece of information into an array of objects - obj.firstname, obj.lastname and so on. Pattern matching is performed on each line.

PS: Im not good with regex but this is working.

Comments

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.