?
Solved

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

Posted on 2004-03-31
6
Medium Priority
?
262 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 2000 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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