while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jure87
    New Member
    • Feb 2008
    • 1

    #1

    while loop

    hello everyone im new to this forum and need some help...i have a java code called flight.java and another code called checkflights.ja va, the purpose of this is to readin a text that is called flights.txt and have the program print out results of flights that i search....HERE IS MY CODE.
    Code:
    public class Flight
    {
        private String orig,dest,day;
        private int depart, arrive;
        private double price;
        
        public Flight(String theOrig, String theDest, String theDay,
            int theDepart, int theArrive, double thePrice)
        {
            orig=theOrig;
            dest=theDest;
            day=theDay;
            depart=theDepart;
            arrive=theArrive;
            price=thePrice;
        }
        
        public String getOrig()
        {   
            return orig;
        }
        
        public String getDest()
        {
         return dest;
        }
        
        public String getDay()
        {
            return day;
        }
        
        public int getDepart()
        {
            return depart;
        }
        
        public int getArrive()
        {
            return arrive;
        }
        
        public double getPrice()
        {
            return price;
        }
        
        public void setPrice(double thePrice)
        {
            price=thePrice;
        }
        public String toString()
        {
            return String.format ("%s %s %s %d %d %f",orig,dest,day,depart,arrive,price);
        }
        public static void main(String[] args)
        {
            Flight a= new Flight("DFW","JFK","MON",1000,1510,200.00);
            
            System.out.println(a.getOrig());
            System.out.println(a.getDest());
            System.out.println(a.getDay());
            System.out.println(a.getDepart());
            System.out.println(a.getArrive());
            a.setPrice(999.99);
            System.out.println(a.getPrice());
            System.out.println(a);
        }
    }
    now for the Checkflights.ja va....
    Code:
    import java.io.*;
    import java.util.*;
    public class CheckFlights
    {
        public static void main (String[]args) throws FileNotFoundException
        {
            Flight[] a= new Flight[417];
            Scanner s= new Scanner(new FileReader("flights.txt"));
            String theOrig="";
            String theDest="";
            String theDay="";
            int theDepart=-1;
            int theArrive=-1;
            double thePrice=-1;
            
            for(int i=0; i< a.length && s.hasNextLine(); i++)
            {
                theOrig=s.next();
                theDest=s.next();
                theDay=s.next();
                theDepart=s.nextInt();
                theArrive=s.nextInt();
                thePrice=s.nextDouble();
                a[i]= new Flight(theOrig,theDest,theDay,theDepart,
                    theArrive,thePrice);
                    
           }
        
           for(int i=0;i<a.length &&a[i] != null;i++)
           {
               System.out.println(a[i]);
           }
           
           Scanner con= new Scanner (System.in);
           System.out.println("input origin that will be searched: ");
           String origToFind= con.next();
           System.out.println();
           System.out.println("input destination to search");
           String destToFind=con.next();
           System.out.println();
           String dayToFind=con.next();
           System.out.println("input day to search");
        }
    }
    and now for my text file called flights.txt

    DFW JFK MON 1000 1510 200.00
    DFW JFK MON 1400 1910 200.00
    DFW JFK TUE 1000 1510 200.00
    DFW JFK TUE 1400 1910 200.00
    DFW JFK WED 1000 1510 200.00
    DFW JFK WED 1400 1910 200.00
    DFW JFK THU 1000 1510 200.00
    <A massive amount of text has been removed, because it caused the message to disappear.

    MODERATOR>
    .....what i need my checkflights.ja va to do when it is run is to produce output that tells me the flight time and price from the txt file when i search for a specific flight....i believe this needs a while loop, can anyone help me
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    When the source, dest and day of two flights are all equal you consider the two
    flights to be equal. Read all about it in the API docs for the equals() and
    hashCode() methods (which you have to implement in your Flight class)
    in the Object API documentation.

    And yes indeed you need some form of a loop to search through your array for
    one flight being equal to another one. A for-loop would do fine here.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      For overriding equals and hashCode, you can also read this article.

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        .....what i need my checkflights.ja va to do when it is run is to produce output that tells me the flight time and price from the txt file when i search for a specific flight....i believe this needs a while loop, can anyone help me
        When you use while loop for a long iterations, That may decreases the performance, if yes, then try to compare it with 0...


        Code:
        while( any <= 0){
               statements;....
        }
        hope it helps too,
        Sukatoa

        Comment

        • Doegon
          New Member
          • Feb 2008
          • 14

          #5
          yeh you are write a loop can be used,if you know anithing about binary search or linear search try either of them and i reccomend binary search coz its effective and fast.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Doegon
            yeh you are write a loop can be used,if you know anithing about binary search or linear search try either of them and i reccomend binary search coz its effective and fast.
            Hashing (java.util.Hash Map) would be faster, and I suspect easier, too.

            Comment

            Working...