473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

while loop

1
hello everyone im new to this forum and need some help...i have a java code called flight.java and another code called checkflights.java, 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.
Expand|Select|Wrap|Line Numbers
  1. public class Flight
  2. {
  3.     private String orig,dest,day;
  4.     private int depart, arrive;
  5.     private double price;
  6.  
  7.     public Flight(String theOrig, String theDest, String theDay,
  8.         int theDepart, int theArrive, double thePrice)
  9.     {
  10.         orig=theOrig;
  11.         dest=theDest;
  12.         day=theDay;
  13.         depart=theDepart;
  14.         arrive=theArrive;
  15.         price=thePrice;
  16.     }
  17.  
  18.     public String getOrig()
  19.     {   
  20.         return orig;
  21.     }
  22.  
  23.     public String getDest()
  24.     {
  25.      return dest;
  26.     }
  27.  
  28.     public String getDay()
  29.     {
  30.         return day;
  31.     }
  32.  
  33.     public int getDepart()
  34.     {
  35.         return depart;
  36.     }
  37.  
  38.     public int getArrive()
  39.     {
  40.         return arrive;
  41.     }
  42.  
  43.     public double getPrice()
  44.     {
  45.         return price;
  46.     }
  47.  
  48.     public void setPrice(double thePrice)
  49.     {
  50.         price=thePrice;
  51.     }
  52.     public String toString()
  53.     {
  54.         return String.format ("%s %s %s %d %d %f",orig,dest,day,depart,arrive,price);
  55.     }
  56.     public static void main(String[] args)
  57.     {
  58.         Flight a= new Flight("DFW","JFK","MON",1000,1510,200.00);
  59.  
  60.         System.out.println(a.getOrig());
  61.         System.out.println(a.getDest());
  62.         System.out.println(a.getDay());
  63.         System.out.println(a.getDepart());
  64.         System.out.println(a.getArrive());
  65.         a.setPrice(999.99);
  66.         System.out.println(a.getPrice());
  67.         System.out.println(a);
  68.     }
  69. }
now for the Checkflights.java....
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. public class CheckFlights
  4. {
  5.     public static void main (String[]args) throws FileNotFoundException
  6.     {
  7.         Flight[] a= new Flight[417];
  8.         Scanner s= new Scanner(new FileReader("flights.txt"));
  9.         String theOrig="";
  10.         String theDest="";
  11.         String theDay="";
  12.         int theDepart=-1;
  13.         int theArrive=-1;
  14.         double thePrice=-1;
  15.  
  16.         for(int i=0; i< a.length && s.hasNextLine(); i++)
  17.         {
  18.             theOrig=s.next();
  19.             theDest=s.next();
  20.             theDay=s.next();
  21.             theDepart=s.nextInt();
  22.             theArrive=s.nextInt();
  23.             thePrice=s.nextDouble();
  24.             a[i]= new Flight(theOrig,theDest,theDay,theDepart,
  25.                 theArrive,thePrice);
  26.  
  27.        }
  28.  
  29.        for(int i=0;i<a.length &&a[i] != null;i++)
  30.        {
  31.            System.out.println(a[i]);
  32.        }
  33.  
  34.        Scanner con= new Scanner (System.in);
  35.        System.out.println("input origin that will be searched: ");
  36.        String origToFind= con.next();
  37.        System.out.println();
  38.        System.out.println("input destination to search");
  39.        String destToFind=con.next();
  40.        System.out.println();
  41.        String dayToFind=con.next();
  42.        System.out.println("input day to search");
  43.     }
  44. }
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.java 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
Feb 29 '08 #1
5 1700
JosAH
11,448 Expert 8TB
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
Feb 29 '08 #2
r035198x
13,262 8TB
For overriding equals and hashCode, you can also read this article.
Feb 29 '08 #3
sukatoa
539 512MB
.....what i need my checkflights.java 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...


Expand|Select|Wrap|Line Numbers
  1. while( any <= 0){
  2.        statements;....
  3. }
hope it helps too,
Sukatoa
Feb 29 '08 #4
Doegon
14
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.
Mar 2 '08 #5
BigDaddyLH
1,216 Expert 1GB
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.HashMap) would be faster, and I suspect easier, too.
Mar 3 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
10
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.