• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

programm doesnt quit after being executed!!!! 4 classes

I have a problem with my programm..when I execute it, then it doesnt quit...it shows all results but is supposed to quit when it is done...please am out of ideas!!!!

It has 4 classes...1. Fahrzeug(car)
                         2. FahrzeugFileReader (reads informations from file)
                        3.  FuhrparkVerwaltung (puts the informations into hashtable and
                            gives result back) as average age, age and so on
                         4. TestFuhrVerwaltung...(to execute the programm)

1 class:  Car

import java.util.*;

public class Fahrzeug
{

      private String fahrzeugbez;
      private String zulassung;
        private String Kilomstand;
        private String fahrgestellnr;
      GregorianCalendar c;

public Fahrzeug(String fahrzeugbez, String fahrgestellnr, String zulassung, String Kilomstand)

  {
      this.fahrzeugbez = fahrzeugbez;
      this.fahrgestellnr = fahrgestellnr;
      this.zulassung = zulassung;
      this.Kilomstand = Kilomstand;

  }


 public String getFahrzeugbez()  /*returns Fahrzeugbezeichung*/
  {
    return fahrzeugbez;
  }



 public String getKilomstand()  /*returns Kilometerstand*/
  {
    return Kilomstand;
  }

 public String getFahrgestellnr() /*returns Fahrgestellnummer*/
  {
    return fahrgestellnr;
  }  

 public int getAlter()   /*returns Age in years*/

  {
   int alter = 0;
 
   

      c = new GregorianCalendar();
   int tag = c.get(c.DAY_OF_MONTH);
   int monat = c.get(c.MONTH)+ 1;
   int jahr = c.get(c.YEAR);

   //format TTMMJJJJ

   int autoTag = Integer.parseInt(zulassung.substring(0,2));
   int autoMonat = Integer.parseInt(zulassung.substring(2,4));
   int autoJahr = Integer.parseInt(zulassung.substring(4,8));

   if((jahr == autoJahr) && ((monat == autoMonat && tag < autoTag) || (monat < autoMonat)))
    {
     alter = jahr - autoJahr - 1;
    }
 
   else
    {
     alter = jahr - autoJahr;
    }
   

   
   return alter;
 
  }

  public void print() /* prints out details of class Fahrzeug*/
  {
      System.out.println( "car " +
                                 getAlter() + " " +
                                 getFahrgestellnr() + " " +
                                    getFahrzeugbez() + " ist "+
                                    getKilomstand() +" alt");
  }
}


class 2 Read information from class Auto

import java.io.*;

public class FahrzeugFileReader {

      BufferedReader br; /* makes new BufferedReader*/

   public FahrzeugFileReader(String filename) throws FileNotFoundException
   {
         br = new BufferedReader(
                                    new InputStreamReader(
                                    new FileInputStream(filename)));
                                    
     }
     
     public Fahrzeug read()
     {
           Fahrzeug bill = null;
           try
           {
     
            String fahrzeugbez, zulassung, Kilomstand, fahrgestellnr;
             fahrzeugbez = br.readLine();
             zulassung = br.readLine();
             Kilomstand = br.readLine();
             fahrgestellnr = br.readLine();
            if (fahrzeugbez==null || zulassung==null || Kilomstand==null || fahrgestellnr==null) return null;

             bill = new Fahrzeug(fahrzeugbez, zulassung, Kilomstand, fahrgestellnr);

           }
            catch (IOException e) {
            System.out.println("Error while reading");
            }
            return bill;
        }

     public void close() throws IOException
     {
      br.close();      
     }

      }

class 3: puts it in hashtable in caculates some info about age and more

import java.util.*;
public class FuhrparkVerwaltung
 {
      
      Hashtable Hash = new Hashtable(); /*makes new hashtable*/
      FahrzeugFileReader reader;
      Fahrzeug Auto = null;

      public FuhrparkVerwaltung(String filename)  /*connects Reader to file*/
        {

         try
          {
                  reader = new FahrzeugFileReader(filename);
            }
            catch (Exception e)
             {
                  System.out.println("File not found");
             }
        
      
            try
               {
                  do
                   {
                        Auto = reader.read();
                        if (Auto!=null)
                         {
                          Hash.put(Auto.getFahrgestellnr(),Auto);
                         }
                        }
                        while (Auto != null);
                  }
            catch (Exception e)
             {
           System.out.println("Failure in Datastructure");
             }
      
        }

  /*Returns number of cars in Hashtable*/
 
  public int getnumber()
   {
          return Hash.size();
   }
   
   /*Returns average age of cars in hashtable*/
   public double averageAge()
   {
         double average = 0;
         Fahrzeug Fahr = null;
         Enumeration e = Hash.elements();
         while(e.hasMoreElements())
         {
           Fahr = (Fahrzeug)e.nextElement();
           average = average + Fahr.getAlter();
    }
      return average/getnumber();
  }
 
 
  public int young()   /*Returns theyoungest car in hashtable*/
  {
        Fahrzeug Fahr = null;
        int young=999, age=0;
        Enumeration e = Hash.elements();
        while(e.hasMoreElements())
        {
              Fahr = (Fahrzeug)e.nextElement();
              age = Fahr.getAlter();
              if(age < young)
                    young = age;
     }
        return young;
  }
 
  public int old() /*Returns the oldest car in hashtable*/
  {
        Fahrzeug Fahr = null;
        int old = 0, age = 0;
        Enumeration e = Hash.elements();
        while(e.hasMoreElements())
        {
              Fahr = (Fahrzeug)e.nextElement();
              age = Fahr.getAlter();
              if(age > old)
              {
                    old = age;
              }
        }
        return age;
  }
 
  public int underAverage() /*Returns number of cars which have age under average*/
  {
        Fahrzeug Fahr = null;
        int age = 0, count = 0;
        double average = averageAge();
        Enumeration e = Hash.elements();
        while(e.hasMoreElements())
        {
              Fahr = (Fahrzeug)e.nextElement();
              age = Fahr.getAlter();
              if(age<average)
              {
                    count++;
              }
              
              
        }
        return count;
  }
 
  public int overAverage() /*Returns number of cars which have age over average*/
  {
        Fahrzeug Fahr = null;
        int age = 0, count = 0;
        double average = averageAge();
        Enumeration e = Hash.elements();
        while(e.hasMoreElements())
        {
              Fahr = (Fahrzeug)e.nextElement();
              age = Fahr.getAlter();
              if(age>average)
              {
                    count++;
              }
        }
        return count;
        
  }
 
  public int averageKilometer()  /*Returns the car which has most kilometers per year*/
  {
        Fahrzeug Fahr = null;
        String stringKilometer;
        int Kilometer = 0;
        int AverageKilometer;
        int mostKilometer = 0;
        Enumeration e = Hash.elements();
        while(e.hasMoreElements());
        {
            int kilomInt;
              Fahr = (Fahrzeug)e.nextElement();
              stringKilometer = Fahr.getKilomstand();
            
              kilomInt = Integer.parseInt(stringKilometer);
            
              AverageKilometer = (kilomInt/Fahr.getAlter());
              if(AverageKilometer > mostKilometer)
              {
                    mostKilometer = AverageKilometer;
              }
        }
        return mostKilometer;
  }

}


and the last class  to execute the programm:

public class TestFuhrparkVerwaltung {

      public static void main(String[] args) {
            FuhrparkVerwaltung fpv;
            System.out.println("***********************************************");
            System.out.println("            FahrzeugVerwaltung                 ");
            System.out.println("***********************************************");

             fpv = new FuhrparkVerwaltung(args[0]);
            

             System.out.println("Total number of cars=" + fpv.getnumber());
            System.out.println("Average age of all cars=" + fpv.averageAge());
            System.out.println("Youngest car=" + fpv.young());
          System.out.println("oldest car=" + fpv.old());
            System.out.println("Number of cars which are younger that average=" + fpv.underAverage());
            System.out.println("Number of cars which are older that average=" + fpv.overAverage());
            System.out.println("Car with most Kilometers per year=" + fpv.averageKilometer());
            }

      }
0
va80
Asked:
va80
1 Solution
 
Thunder_screamCommented:
try System.exit(0); at the end of your main..when your done..
0
 
mmuruganandamCommented:
in class FuhrparkVerwaltung,


      public int averageKilometer() /*Returns the car which has most kilometers per year*/
      {
            Fahrzeug Fahr = null;
            String stringKilometer;
            int Kilometer = 0;
            int AverageKilometer;
            int mostKilometer = 0;
            Enumeration e = Hash.elements();
            while (e.hasMoreElements())
            {
                  int kilomInt;
                  Fahr = (Fahrzeug) e.nextElement();
                  stringKilometer = Fahr.getKilomstand();

                  kilomInt = Integer.parseInt(stringKilometer);

                  AverageKilometer = (kilomInt / Fahr.getAlter());
                  if (AverageKilometer > mostKilometer)
                  {
                        mostKilometer = AverageKilometer;
                  }
            }
            return mostKilometer;
      }


>>             while (e.hasMoreElements());  
while (###) should not end with semi-colon

remove that.


It will work.


Regards,
Muruga
0
 
mmuruganandamCommented:
while (e.hasMoreElements());  

This causes, the loop to be never ending.

So removing the ; will do the work for you.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mayank SAssociate Director - Product EngineeringCommented:
:-) Easy money.
0
 
zzynxSoftware engineerCommented:
>> it shows all results
Except for the last one I expect
0
 
va80Author Commented:
wow thanks that was fast....thanks allot..I really appreciate it...needed it a.s.a.p  :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now