提问人:Vishal 提问时间:6/30/2023 最后编辑:Sajid MehmoodVishal 更新时间:7/1/2023 访问量:25
如何在 Java 中使用 Scanner 将值存储在自定义对象数组中?
How to store values in custom object array using Scanner in Java?
问:
我参加了一次考试,我必须创建一个类,之后,我必须编写两个函数,其中输入参数是一个对象数组,并从用户输入中获取值。下面是我写的代码。但是,我在第 17 行和第 18 行中重复得到了 NullPointerException 和 InputMismatchException。请帮帮我。
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//code to read values
//code to call required method
//code to display the result
Scanner sc = new Scanner(System.in);
int tasks = sc.nextInt();
Bank[] banks = new Bank[tasks];
for(int i = 0; i < tasks; i++){
int bankId = sc.nextInt();
String bankName = sc.next();
int numberOfCustomers = sc.nextInt();
// banks[i].setNumberOfCustomers(numberOfCustomers);
String city = sc.nextLine();
// banks[i].setCity(city);
banks[i] = new Bank(bankId, bankName, numberOfCustomers, city);
}
String city = sc.next();
if(findAvgNumberOfCustomersByCity(banks, city) != 0){
System.out.println(findAvgNumberOfCustomersByCity(banks, city));
}
else{
System.out.println("No Bank found with matching criteria.");
}
String ans = getBankWithMinimumNoOfCustomers(banks);
if(ans.equals(null)){
System.out.println("No Bank found with matching criteria.");
}
else{
for(Bank b : banks){
if(b.getBankName().equals(ans)){
System.out.println(b.getBankId());
System.out.println(b.getBankName());
System.out.println(b.getNumberOfCustomers());
System.out.println(b.getCity());
}
}
}
}
//code the first method
public static double findAvgNumberOfCustomersByCity(Bank[] banks, String city){
double ans = 0;
double total = 0, totalBanks = 0;
for(Bank b : banks){
if(b.getCity().equals(city)){
total += b.getNumberOfCustomers();
totalBanks++;
}
}
return (total/totalBanks);
}
//code the second method
public static String getBankWithMinimumNoOfCustomers(Bank[] banks){
int minNumber = Integer.MAX_VALUE;
String bankName = "";
for(Bank b : banks){
if(b.getNumberOfCustomers() < minNumber){
minNumber = b.getNumberOfCustomers();
bankName = b.getBankName();
}
}
if(minNumber == Integer.MAX_VALUE){
return null;
}
else{
return bankName;
}
}
}
//code the class
class Bank{
private int bankId;
private String bankName;
private int numberOfCustomers;
private String city;
public Bank(int bankId, String bankName, int numberOfCustomers, String city){
this.bankId = bankId;
this.bankName = bankName;
this.numberOfCustomers = numberOfCustomers;
this.city = city;
}
public int getBankId(){
return this.bankId;
}
public void setBankId(int bankId){
this.bankId = bankId;
}
public String getBankName(){
return this.bankName;
}
public void setBankName(String bankName){
this.bankName = bankName;
}
public int getNumberOfCustomers(){
return this.numberOfCustomers;
}
public void setNumberOfCustomers(int numberOfCustomers){
this.numberOfCustomers = numberOfCustomers;
}
public String getCity(){
return this.city;
}
public void setCity(String city){
this.city = city;
}
}
任何人都可以帮助我找到代码中的错误,我很困惑,并尝试了很多方法来获得答案,但无法得到答案。
答:
0赞
Vishal
7/1/2023
#1
当我尝试存储值时,问题出在 main 方法中。这是不恰当的。正确的方法是——
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of banks: ");
int numBanks = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
Bank[] banks = new Bank[numBanks];
for (int i = 0; i < numBanks; i++)
int bankId = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
String bankName = scanner.nextLine();
int customers = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
String bankCity = scanner.nextLine();
banks[i] = new Bank(bankId, bankName, customers, bankCity);
}
String cityToFind = scanner.nextLine();
}
这是正确的方法。
评论