Solved

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

Posted on 2004-03-31
6
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 40
ejb stateless example 2 44
What browser will run Java? 7 128
Delete .class files at every compile 4 26
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

730 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