程序永远运行但不显示任何内容

1 java

import java.util.*;
import java.io.*;
public class ConcreteClass
{
    private String fileName = "List.txt";
    private Customer[] clients = null;

    public static void main(String[] args) throws Exception
    {

        ConcreteClass first = new ConcreteClass();


        first.readFile();
        first.showCustomers();


        System.out.println("Hello World");

    }

    public void showIntro()
    {
        System.out.println("---------BANKING MANAGEMENT PROGRAM---------");
        System.out.println("1---Show list of customers.");
        System.out.println("2---Add a Customer bank account ");
        System.out.println("3---Remove a Customer bank account ");
        System.out.println("4---Sort customer list according to name");
        System.out.println("5---Sort customer list according to account balance");
    }

    public void readFile() throws Exception
    {   
        int index = 0;
        Scanner reader = new Scanner(new File(fileName));

        int count=0;

        while(reader.hasNextLine())
        {
            count++;
        }


        while(reader.hasNextLine())
        {
            String[] roster = reader.nextLine().split(",");

            String fName = roster[0];
            String lName = roster[1];
            String mName = roster[2];
            double balance = Double.parseDouble(roster[3]);
            String accNo = roster[4];

            Account b = new Account(balance,accNo);
            Customer c = new Customer(fName,lName,mName,b);

            clients = new Customer[count];
            clients[index++] = c;
        }

        reader.close();

    }

    public void showCustomers()
    {
        for(int i =0; i<clients.length; i++)
        {
            System.out.println(clients[i].toString());
        }
    }



}
Run Code Online (Sandbox Code Playgroud)
Account(balance,account number)-toString(): ("Account Number: "+getNumber()+"/n Account Balance: "+getBalance())

Customer(fName,lName,mName,account)-toString(): ("Customer Name:"+getFirstName()+" "+getMidName()+" "+getLastName()+"  Customer Account:"+getAccount()) 
Run Code Online (Sandbox Code Playgroud)

Sha*_*dov 7

在你的:

while(reader.hasNextLine())
{
  count++;
}
Run Code Online (Sandbox Code Playgroud)

你只是在检查读者是否有下一行,但你不会进一步.我的意思是想象你在第一行,并检查是否有第二行.然后在下一次迭代中,您仍然在第一行.你需要更进一步,像这样:

reader.nextLine()

此外,如果你在第一次这样做,while那么你的第二次while不会运行,你需要以其他方式处理.另外一件事 - 你在使用这个数组(客户端)所做的事情没有意义,你需要在这些循环之前创建该数组.