0

I was given an exam, where I had to create a class and after that, I had to write two functions in which the input parameter is an object Array and took the values from user input. Below is the code that I have written. But, I repetitively got NullPointerException and InputMismatchException in line number 17 & 18. Please help me.

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;
    }
}

Anyone please help me in finding the error in my code, I was flummoxed and have tried a lot to get the answer, but unable to get so.

1 Answer 1

0

The problem is in the main method when I am trying to store the values. That was not appropriate. The correct way is-

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();
}

This is the correct way to do so.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.