gpt4 book ai didi

java - java读写数据报错

转载 作者:行者123 更新时间:2023-11-29 06:36:12 24 4
gpt4 key购买 nike

线程“main”中的异常 java.lang.NullPointerException 在 MovieSeating.assignCustomerAt(MovieSeating.java:27) 在 Assignment8.main(Assignment8.java:84)

在我从包含名字和姓氏列表的 txt 文件中提取数据后,在我尝试将名称分配给二维数组范围内的某个元素后,它给出了错误

谢谢!


    public class Customer
{
private String lastName;
private String firstName;

// This constructor sets the first name and last name to "???�
public Customer()
{
lastName = "???";
firstName = "???";
}

// This constructor constructs a Customer object given the last name and first name
public Customer(String customerInfo)
{
int space = customerInfo.indexOf(" ");
firstName = customerInfo.substring(0, space).trim();
lastName = customerInfo.substring(space+1).trim();

}

// This constructor cConstructs a Customer object using the string containing customer's info.
// It uses the StringTokenizer to extract first name, last name, id, the number of matinee tickets,
// and the number of normal tickets.
public Customer(String lName, String fName)
{
lastName = lName;
firstName = fName;

}

// This method sets the last name.
public void setLastName(String lName)
{
lastName = lName;
}
// This method sets the first name.
public void setFirstName(String fName)
{
firstName = fName;
}

// This method returns the last name.
public String getLastName()
{
return lastName;
}
// This method returns the first name.
public String getFirstName()
{
return firstName;
}

// This method checks if a customer object passed as a parameter and itself (customer object)
// are same using their last names and first names.
public boolean equals(Customer other)
{
if (lastName.equals(other.lastName) && firstName.equals(other.firstName))
return true;
else
return false;
}

// This method returns a string containing a customer's initials
// (first characters of firstName and lastName.)
public String toString()
{
String result = firstName.charAt(0) + "." + lastName.charAt(0) + ".";
return result;
}


} // end of the class Customer

    class MovieSeating 
{
private String[][] Seats;
public MovieSeating(int rowNum, int columnNum)
{
String [][] Seats = new String[rowNum][columnNum];
for (int r = 0; r < rowNum; r++)
{
for (int c = 0; c < columnNum; c++)
{
Seats[r][c] = "?.?";
}
}
}

private Customer getCustomerAt(int row, int col)
{
System.out.println("Customer at row " + row + " and col " + col + "." );
System.out.println(Seats[row][col]);

}

public boolean assignCustomerAt(int row, int col, Customer tempCustomer)
{
if (Seats[row][col].equals("?.?"))
{
tempCustomer = Seats[row][col];
return true;
}
else {
System.out.println("Seat taken..");
return false;
}

}

public boolean checkBoundaries(int row, int col)
{
if (col < 0 || row < 0)
{
return false;
}
else {
return true;
}
}
}

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

public class Assignment8
{
public static void main(String[] args) throws IOException
{

MovieSeating theatreSeating;
Customer tempCustomer;
int requestedRow, requestedCol, row, col, rowNum, columnNum;
String line, fileName;

// to read input from a KEYBOARD.
Scanner stdin = new Scanner(System.in);

// Ask a user to enter a number of rows for a movie theatre seating from a KEYBOARD.
System.out.println("Please enter a number of rows for a movie theatre seating.");
rowNum = stdin.nextInt();

// Ask a user to enter a number of columns for a movie theatre seating from a KEYBOARD.
System.out.println("Please enter a number of columns for a movie theatre seating.");
columnNum = stdin.nextInt();

// instantiate a MovieSeating object
theatreSeating = new MovieSeating(rowNum, columnNum);

// get a file name read from a KEYBOARD.
System.out.println("Please enter a file name");
fileName = stdin.next();

// create FileReader and BufferedReader object to
// read from a file.
FileReader fr = new FileReader (fileName);
BufferedReader inFile = new BufferedReader (fr);

/*** reading a customer's information from a FILE ***/
line = inFile.readLine();

/*** we will read line by line until we read the end of a given file ***/
while (line != null)
{
System.out.println("\nA customer information is read from a file.");
// printing information read from a file.
System.out.println(line);

// creating a customer object using information from a file
tempCustomer = new Customer(line);

// Ask a user to decide where to seat a customer by asking for row and column of a seat
System.out.println("Please enter a row number where the customer wants to sit.");
requestedRow = stdin.nextInt();
row = requestedRow -1;

System.out.println("Please enter a column number where the customer wants to set.");
requestedCol = stdin.nextInt();
col = requestedCol -1;

// Checking if the row number and column number are valid (exist in the theatre that we created.)
if (theatreSeating.checkBoundaries(row, col) == false)
{
System.out.println("\nrow or column number is not valid.");
System.out.println("A customer " + tempCustomer.getFirstName() + " " + tempCustomer.getLastName() + " is not assigned a seat.");
}
else
{
// Assigning a seat for a customer
if (theatreSeating.assignCustomerAt(row, col, tempCustomer) == true)
{
System.out.println("\nThe seat at row " + row + " and column " + col + " is assigned to the customer " + tempCustomer.toString());
System.out.println(theatreSeating);
}
else
{
System.out.println("\nThe seat is taken.");
}
}
// Read next line in a FILE
line = inFile.readLine();

}//end of the while loop
// Closing the file
inFile.close();

}

}

最佳答案

好的,这是你的问题:

您有一个名为seats 的数组。它是 String 类型。所以它可以容纳像“???”这样的字符串。或“免费座位”。但是,您尝试将客户放入其中。这正是错误消息所说的:error: incompatible types tempCustomer = Seats[row][col]; ^ 必需:客户发现:字符串 1 错误

现在您有几个选择。一个非常简单的方法是将数组的类型更改为 Customers。而不是输入“???”在空闲座位上,什么也不放。创建数组后,每个“槽”都是null,其实就是“空”的意思。

顺便说一句:您违反了许多编写 Java 代码的建议规则。这只是一些简单的事情,不会影响您的代码(无论如何它都会运行;)。但尊重这些不成文的规则是一个很好的做法(比如将 { 与函数头放在同一行,或者用大写字母命名变量 no)。

关于java - java读写数据报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com