Solved

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

Posted on 2004-03-31
6
256 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
ID: 10722075
try System.exit(0); at the end of your main..when your done..
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 500 total points
ID: 10722084
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
ID: 10722089
while (e.hasMoreElements());  

This causes, the loop to be never ending.

So removing the ; will do the work for you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 10722155
:-) Easy money.
0
 
LVL 37

Expert Comment

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

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

919 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

16 Experts available now in Live!

Get 1:1 Help Now