Solved

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

Posted on 2004-03-31
6
255 Views
Last Modified: 2012-05-04
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
Comment
Question by:va80
6 Comments
 
LVL 2

Expert Comment

by:Thunder_scream
Comment Utility
try System.exit(0); at the end of your main..when your done..
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 500 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
while (e.hasMoreElements());  

This causes, the loop to be never ending.

So removing the ; will do the work for you.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
:-) Easy money.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> it shows all results
Except for the last one I expect
0
 

Author Comment

by:va80
Comment Utility
wow thanks that was fast....thanks allot..I really appreciate it...needed it a.s.a.p  :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now