需要帮助创建一个程序,找到最轻和最重的狗

时间:2014-09-10 21:00:13

标签: java arraylist accessor modifier

我在这个Java项目中遇到了很多麻烦。我必须写一个跟踪狗的名字,品种,日期和重量的课程,我必须输入一个包含每条狗一行的文件。我需要一个访问器,修饰符,Arraylist和toString方法。然后主程序需要确定最轻的狗和最重的狗。

import java.io.*;
import java.util.*;
import java.util.ArrayList;

public class Kennel
{

 public static void main(String args[]) 
 {

  String line = ""; // string var to hold entire line

  if (args.length < 1)
   {
    System.out.println("\n   You forgot to put a file name on the command line.");
    System.exit(1);
   };


   String inFile = args[0];  // file name off command line
   Scanner sc = null;
    try
   {
   sc = new Scanner(new File(inFile));
   }
   catch (Exception e)
   {
    System.out.println("File not found");
    System.exit(1);
     }

     // print a message explaining the purpose of the program.
    System.out.println("\nThis program inputs file ");
    System.out.println(args[0]);
    System.out.println("and outputs information about all the dogs in the registry.");
    System.out.println("\nWritten by Phil Trout.");
    System.out.println();
    System.out.println("Property           "+"Name                   "+"Value         ");
    System.out.println();
    // Loop

   //double heaviest = 0.0;
   //double lightest = 1000.0;  

     while(sc.hasNextLine())
    {

    // read a line from the input file via sc into line
    line = sc.nextLine();

    }

    {
    //got from  email infromation   


    StringTokenizer stk = new StringTokenizer(line);
    String name = stk.nextToken();
    String breed = stk.nextToken();
    int month = Integer.parseInt(stk.nextToken());
    int day = Integer.parseInt(stk.nextToken());
    int year = Integer.parseInt(stk.nextToken());
    double weight = Double.parseDouble(stk.nextToken());



    Dog list = new Dog(name, breed, month, day, year, weight);

    ArrayList<Dog> dogs = new ArrayList<Dog>();
    dogs.add(list);

    double firstweight = dogs.get(0).getWeight();

    System.out.println(firstweight);         


    for (int i = 0; i< dogs.size(); i++)
   {
   double newweight = dogs.get(i).getWeight();       
   }








 public class Dog
 {
  private String dogName;
  private String dogBreed;
  private int dogMonth;
  private int dogDay;
  private int dogYear;
  private double dogWeight;

public Dog(String name, String breed, int month, int day, int year, double weight)
{
  dogName = name;
  dogBreed = breed;
  dogMonth = month;
  dogDay = day;
  dogYear = year;
  dogWeight = weight;
 } 
  public String getName()
{
 return dogName;
 }

public String getBreed()
{
return dogBreed;
}
public String toString()
{
return String.format("%d/%d/%d", dogMonth, dogDay, dogYear);





}
public double getWeight()
{
return dogWeight;
}

1 个答案:

答案 0 :(得分:1)

有些事情你做错了,但是你把它放在了错误的地方。 a)从线中提取值,b)创建dog实例,c)将狗添加到列表中,这一切都应该在循环内部发生。否则,您将只获取文件中的最后一个值。

   ArrayList<Dog> dogs = new ArrayList<Dog>(); 

 while(sc.hasNextLine())
{

// read a line from the input file via sc into line
  line = sc.nextLine();
  StringTokenizer stk = new StringTokenizer(line);
  String name = stk.nextToken();
  String breed = stk.nextToken();
  int month = Integer.parseInt(stk.nextToken());
  int day = Integer.parseInt(stk.nextToken());
  int year = Integer.parseInt(stk.nextToken());
  double weight = Double.parseDouble(stk.nextToken());

  Dog list = new Dog(name, breed, month, day, year, weight);

  dogs.add(list);

}

<强>更新

 int lightestIndex = 0;
 int heaviestIndex = 0;

for (int i = 0; i< dogs.size(); i++)
{
   if(dogs.get(i).getWeight() <= dogs.get(lightestIndex).getWeight()){
      lightestIndex = i;
   } 

   if(dogs.get(i).getWeight() >= dogs.get(heaviestIndex).getWeight()){
      heaviestIndex = i;
   }        
}


 System.out.println("The Lightest dog is " + dogs.get(lightestIndex).toString());
 System.out.println("The Heaviest dog is " + dogs.get(heaviestIndex).toString());