当前位置: 首页 > 知识库问答 >
问题:

在java中如何在新行上打印数组元素并修复空指针异常

长孙智刚
2023-03-14
public class Vehicle{
   public String regNo;
   public String make;
   public int yearOfManufacture;
   public double value;
   public Vehicle(String regNo, String make, int yearOfManufacture, double value){
      this.regNo = regNo;
      this.make = make;
      this.yearOfManufacture = yearOfManufacture;
      this.value = value;
   }
   public String getRegNo(){
      return regNo;
   }
   public String getMake(){
      return make;
   }
   public int getYearOfManufacture(){
      return yearOfManufacture;
   }
   public double getValue(){
      return value;
   }
   public void setValue(double value){
      this.value = value;
   }
   public int calculateAge(int currentYear){
      int age;
      age = currentYear - yearOfManufacture;
      return age;
   }
   public String toString(){
      String V = "Register number is " +regNo+ ", make " +make+ ", year of manufacture " +yearOfManufacture+ ", and it costs " +value;
      return V;
   }
  }
public class SecondHandVehicle extends Vehicle{
   private int numberOfOwners;
   public SecondHandVehicle(String regNo, String make, int yearOfManufacture, double value, int numberOfOwners){
      super(regNo, make, yearOfManufacture, value);
      this.numberOfOwners = numberOfOwners;
   }
   public int getNumberOfOwners(){
      return numberOfOwners;
   }
   public boolean hasMultipleOwners(){
      if (numberOfOwners > 1)
         return true;
      else
         return false;
   }
   public String toString(){
      String SHV = "Register number is " +regNo+ ", make " +make+ ", year of manufacture " +yearOfManufacture+ ", and it costs " +value+ ", it has " +numberOfOwners+ " owner/s";
      return SHV;
   }
}

主要方法:

import java.util.*;
public class TestVehicle{
   public static void main(String [] args){
      Scanner scnr = new Scanner(System.in);
      String regNo, make;
      int yearOfManufacture, numberOfOwners;
      double value;
      Vehicle V[] = new Vehicle[50];
      SecondHandVehicle SHV[] = new SecondHandVehicle[50];
      int choice = 0;
      int v = 0;
      int s = 0;
      while (choice != 5) {
         System.out.println(
                    " What do you want to do?\n\n"
                    + " 1. Add a vehicle to the list V.\n"
                    + " 2. Display the details of all vehicles available in V\n"
                    + " 3. Add a second-hand vehicle to the list SHV.\n"
                    + " 4. Display the details of all second-hand vehicles available in SHV with only one owner.\n"
                    + " 5. Exit the program.");
         choice = scnr.nextInt();
         if(choice == 1){
            System.out.println(" Enter the register number of the Vehicle: ");
            regNo = scnr.next();
            System.out.println(" Enter the make of the Vehicle: ");
            make = scnr.next();
            System.out.println(" Enter the year of manufacture of the Vehicle: ");
            yearOfManufacture = scnr.nextInt();
            System.out.println(" Enter the value of the Vehicle: ");
            value = scnr.nextDouble();
            V[v] = new Vehicle(regNo, make, yearOfManufacture, value);
            v++;
         }
         else if(choice == 2){
            for(int i=0;i<V.length;i++){
               if (V[v]!=null) {
                  System.out.println(V[v]);
               }
            }      
            printVehicles(V, 0);
         }
         else if(choice == 3){
            System.out.println(" Enter the register number of the Vehicle: ");
            regNo = scnr.next();
            System.out.println(" Enter the make of the Vehicle: ");
            make = scnr.next();
            System.out.println(" Enter the year of manufacture of the Vehicle: ");
            yearOfManufacture = scnr.nextInt();
            System.out.println(" Enter the value of the Vehicle: ");
            value = scnr.nextDouble();
            System.out.println(" Enter the number of owners: ");
            numberOfOwners = scnr.nextInt();
            SHV[s] = new SecondHandVehicle(regNo, make, yearOfManufacture, value, numberOfOwners);
            s++;
         }
         else if(choice == 4){
            for(int i=0;i<SHV.length;i++){
               if (SHV[s]!=null) {
                  System.out.println(SHV[s]);
               }
            }      
            printSecondHandVehicles(SHV, 0);
         }
         else if(choice == 5){
            System.out.println(" Thank you. ");
         }
         else{
            System.out.println(" Wrong choice... Try again ");
         }
      }
   }
   public static void printVehicles(Vehicle V[], int i){
      if(i < V.length){
         System.out.print(V[i].toString());
         printVehicles(V, i+1);
          }
   }
   public static void printSecondHandVehicles(SecondHandVehicle SHV[], int i){
      if(i < SHV.length){
         System.out.print(SHV[i].toString());
         printSecondHandVehicles(SHV, i+1);
      }
   }
}

共有1个答案

饶志
2023-03-14

对于选择2,您不需要For循环(您可以在printvehics中检查数组元素是否为null,只需调用:

else if (choice == 2) {
  printVehicles(V, 0);
}

printvehics:

// Arguments validation:
// If V is null, or
// If i is not valid (less than 0 or greater than array length)
// Then return from the function
// Also, acts as base case for recursion
if (V == null || i < 0 || i >= V.length) {
  return;
}
// Print the vehicle only if the array element is not null
if (V[i] != null) {
  System.out.println(V[i].toString());
}
// Print the next vehicle i. e. vehicle at index i + 1
printVehicles(V, i+1);

选项4和printsecondhandvehicles类似

 类似资料:
  • 我正试着数字符串中元音的数目。问题是它编译正确,但当我运行它时,命令窗口显示“countvowels.main(countvowels.java:25)处线程'main'java.lang.NullPointerException”。 是什么导致了这一点,我该如何修复它,我该如何防止它在未来再次发生? 第25行是我的if语句:

  • 在标记项处获取NullPointerException。getKey(),不明白问题出在哪里

  • 我知道如何为数字数据类型执行此操作,但请告诉我的字符串数组。 假设在java中有一个由10个元素组成的字符串数组。一些元素包含单词,但其他元素不包含任何内容。 如何运行只打印非空元素的for循环?

  • 问题内容: 什么是Java空指针异常,它是如何发生的?如何修改? 问题答案: 当我们创建一个引用类型的变量时,实际上是创建了一个指向该变量的一个指针,例如: 这种情况下是,tom 其实是指向Person对象的一个指针。 如果我们访问一个没有初始化的对象时,就会出现空指针异常。例如,我们声明了一个Person对象tom,而没有给tom赋值(初始化),这样再调用tom的方法是就会出现空指针异常。 输出

  • 如果一个数组中的所有元素保存的都是 指针,那么我们就称它为 指针数组。指针数组的定义形式一般为: dataType *arrayName[length]; 的优先级高于 ,该定义形式应该理解为: dataType *(arrayName[length]); 括号里面说明 是一个数组,包含了 个元素,括号外面说明每个元素的类型为 。 除了每个元素的数据类型不同,指针数组和普通数组在其他方面都是一样的