如何初始化用户输入的对象数组的String值?

时间:2017-04-11 01:15:15

标签: java

我有这类PetRecord:

 public class PetRecord 
{
    private String name;
    private int age;
    private int weight;
    public PetRecord(String initialName)
    {
        name = initialName;
        age = 0;
    }

    public void set(String newName)
    {
        name = newName; //age and weight are unchanged.
    }

    public PetRecord(int initialAge)
    {
        name = "No name yet.";
        weight = 0;
        if (initialAge < 0)
        {
            System.out.println("Error: Negative age.");
            System.exit(0);
        }
        else
            age = initialAge;
    }

    public void set(int newAge)
    {
        if (newAge < 0)
        {
            System.out.println("Error: Negative age.");
            System.exit(0);
        }
        else
            age = newAge;
        //name and weight are unchanged.
    }

    public PetRecord(double initialWeight)
    {
        name = "No name yet";
        age = 0;
        if (initialWeight < 0)
        {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        }
        else
            weight = initialWeight;
    }

    public void set(double newWeight)
    {
        if (newWeight < 0)
        {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        }
        else
            weight = newWeight; //name and age are unchanged.
    }

    public PetRecord()
    {
        name = "No name yet.";
        age = 0;
        weight = 0;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public double getWeight()
    {
        return weight;
    }
}

我还有另一个使用PetRecord的课程让用户输入有多少宠物,输入宠物的名字,然后按字母顺序对数组进行排序。我已经找到了排序部分(我认为),但是我的循环设置每个PetRecord对象的名称时遇到了问题。我该如何解决这个问题?

import java.util.Scanner;
public class PetSort {


    public static void selectionSort(PetRecord[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            // can add print statement here to help debug sorting algorithm:
            System.out.print("In selectionSort: ");
            for (int k = 0; k < a.length; k++)
                System.out.print(a[k] + ", ");
            System.out.println();

            int indexOfMin = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j].getName().compareTo(a[indexOfMin].getName()) > 0)
                    indexOfMin = j;
            }
            PetRecord temp = a[i];
            a[i] = a[indexOfMin];
            a[indexOfMin] = temp;
        }
    }


    public static void main(String args[]){
        int i;
        Scanner s = new Scanner(System.in);

        System.out.println("How many pets are there?");
        i = s.nextInt();
        PetRecord[] array = new PetRecord[i];
        System.out.println("Please give the names of the pets: ");
        for (int k = 0; k < array.length; k++){
            // This is the line that I'm trying to step through the array, and set the name of each PetRecord Object to what the user inputs.
            //PetRecord array[i] = new PetRecord(s.nextLine());
        }
        selectionSort(array);


    }
}

2 个答案:

答案 0 :(得分:3)

  

我的循环设置每个名称都有问题   PetRecord对象。我该如何解决这个问题?

您需要在循环中插入println消息,否则用户可能不知道输入所需数据的次数。

另外,你不需要在循环中使用整行:

PetRecord array[i] = new PetRecord(s.nextLine());

这样做会:

array[i] = new PetRecord(s.nextLine());

注意 - array的索引器为k而不是i。如果您使用i对预定义数组进行索引,那么您将获得IndexOutOfBoundsException例外。

示例:

System.out.println("How many pets are there?");
i = s.nextInt();
PetRecord[] array = new PetRecord[i];

for (int k = 0; k < array.length; k++){
    System.out.println("Please give the names of the pets: ");
    array[k] = new PetRecord(s.nextLine());
}

答案 1 :(得分:1)

首先要解决您的问题,您应该测试程序的逻辑并清理代码以使其更有条理:

  int n; // number of pets
  String petName; // name of pet
  Scanner s = new Scanner(System.in);
  System.out.println("How many pets are there?");
  n = s.nextInt();
  PetRecord[] arrayPets = new PetRecord[n]; // create array of PetRecord
  for (int i = 0; i < arrayPets.length; i++){
        System.out.println("Please give the name of the pet: ");
        petName = s.nextLine(); // read each pet name
        arrayPets[i] = new PetRecord(petName); //create petRecord object
  }

你还有另一件事要清理它,PetRecord的构造函数应该是:

// default constructor       
public PetRecord()
{
      name = "";
      age = 0;
      weight = 0;
}
你在课堂上

定义了实例变量:

private int age;
private int weight;

因此您应该将参数化构造函数的参数类型与实例变量相同:

 // parameterized constructor
 public PetRecord(String initialName,int initialAge,int initialWeight)
 {
        name = initialName;
        if (initialAge < 0)
        {
            System.out.println("Error: Negative age.");
            System.exit(0);
        }
        else
            age = initialAge;

         if (initialWeight < 0)
         {
            System.out.println("Error: Negative weight.");
            System.exit(0);
         }
        else
            weight = initialWeight;
 }

如果你想要像你这样重载你的构造函数,你也可以这样做:

// overload constructor with 1 parameter
public PetRecord(String initialName)
{
    name = initialName;
    age = 0;
    weight = 0;
}
// overload constructor with 2 parameters
public PetRecord(int initialAge,int initialWeight)
{
   name = "";
   if (initialAge < 0)
   {
       System.out.println("Error: Negative age.");
       System.exit(0);
   }
   else
       age = initialAge;
   if (initialWeight < 0)
   {
          System.out.println("Error: Negative weight.");
          System.exit(0);
   }
   else
       weight = initialWeight;
}

最后,您只需将setget方法更正为实例变量的相同数据类型。