打印空数组

时间:2016-12-24 12:10:38

标签: java

当我在程序中添加新行程并选择打印所有行程时,它会输出一个空信息,但索引号是正确的

当我添加两次旅行时:

我的期望:

Name: customer name
mobile numer : the number

Name: customer name2
mobile numer : the number2

输出的内容:

Name: 
mobile numer : 

Name: 
mobile numer :

这是我的代码

import java.util.*;
class Trip {

    private static String customerName;
    private static String mobileNo;
    private static String reservationCode;
    public static int totalReservations;
    public static Scanner input=new Scanner (System.in);

    public Trip(){//The constructer
        customerName = " ";
        mobileNo = " ";
    }

    public Trip(String Name, String MobileNo) {
        customerName=Name;
        mobileNo=MobileNo;
    }

    public void setcustomerName(String Name){
        customerName = Name;
    }

    public void setmobileNo(String MobileNo){
        mobileNo = MobileNo;
    }

    public String getcustomerName() {
        return customerName;
    }

    public String getmobileNo() {
        return mobileNo;
    }

    public static void printTripInfo(){

        Trip M=new Trip();
        M.getcustomerName();
        M.getmobileNo();

        System.out.println("Customer Name:"+customerName);
        System.out.println("Phone Number:"+mobileNo);

    }

}//end class trip


public class CabCompany{
    private static int reservations_size=20;
    private static Trip[] trip;
    Trip NewTrip;
    public static int user;

    //the scanners
    public static Scanner textinput=new Scanner (System.in);
    public static Scanner input=new Scanner (System.in);

    public static void main(String[]args){
        trip=new Trip[reservations_size];

        do {
            System.out.println("1. Add a new trip\n2. Find a trip by reservation code\n3. List all trips\n");
            user=input.nextInt();

            if ( user == 1) {
                System.out.println("Name:");
                String Name = textinput.nextLine();

                System.out.println("MobileNo:");
                String MobileNo = input.next();

                boolean flag;
                CabCompany Y = new CabCompany();
                flag = Y.addTrip(Name,MobileNo);

                if( flag == true ) {
                    System.out.println("the trip has been added successfully");

                } else if ( flag == false ){
                    System.out.println("the trip hasn't been added");
                }
            }

            if( user == 2) {
                printAll();
            }

            if( user == 3 ){
                System.out.println("The total Reservations is:"+Trip.totalReservations);
            }

        } while (user != 7);
    }


    public boolean addTrip(String customerName,String mobileNo){

        boolean flag = true;

        if ((mobileNo.length() == 10))
        {


            NewTrip=new Trip(customerName, mobileNo);
            Trip.totalReservations++;
            int i = Trip.totalReservations - 1;
            trip[i] = NewTrip;


            flag = true;
        } else {
            flag = false;
        }

        return flag;
    }

    public static void printAll() {

        for(int t=0; t<Trip.totalReservations; t++){
            trip[t].printTripInfo();

        }
    }
}

2 个答案:

答案 0 :(得分:3)

有很多话要说。我会尽力专注于必要的。

使用单个扫描仪并替换:

     System.out.println("Name:");
     String Name = textinput.nextLine();

     System.out.println("MobileNo:");
     String MobileNo = input.next();

by:

     System.out.println("Name:");
     String Name = textinput.nextLine();

     System.out.println("MobileNo:");
     String MobileNo = textinput.nextLine();

使用两台扫描仪容易出错,我只使用nextLine(),因为next()nextLine()不应与同一台扫描仪混合使用,以防止混合时出现一些副作用。< BR />

printTripInfo()方法中,使方法不是静态的,并删除您声明的Trip实例,因为您在此处调用方法时已经在Trip实例的上下文中:

   for(int t=0; t<Trip.totalReservations; t++){
        trip[t].printTripInfo();
    }

tripTrip个实例的数组。

你写的是:

public static void printTripInfo(){
    Trip M=new Trip();
    M.getcustomerName();
    M.getmobileNo();

    System.out.println("Customer Name:"+customerName);
    System.out.println("Phone Number:"+mobileNo);
}

你应该写下:

public void printTripInfo(){
    System.out.println("Customer Name:"+customerName);
    System.out.println("Phone Number:"+mobileNo);
 }

答案 1 :(得分:1)

public static void printAll() {
    for(int t=0; t<Trip.totalReservations; t++){
        trip[t].printTripInfo();
    }
} 

在这里,您在数组的每个实例上调用printTripInfo()方法,但您没有使用该实例。相反,每次调用Trip方法时,您都会创建printTripInfo()的新实例。 所以你需要改变你的方法

public static void printTripInfo(){
    Trip M=new Trip();
    M.getcustomerName();
    M.getmobileNo();
    System.out.println("Customer Name:"+customerName);
    System.out.println("Phone Number:"+mobileNo);
}

public void printTripInfo(){
    System.out.println("Customer Name:"+ this.getcustomerName());
    System.out.println("Phone Number:"+ this.getmobileNo());
}

但是当您尝试使用上述更改运行代码时,它只是无法编译。因为您尝试从非静态方法访问静态字段customerNamemobileNo。因此,您还需要将customerNamemobileNo字段更改为非静态字段。