-4

このプログラムを実行しようとすると、「読み込みレコード リストの開始」のみが表示されます。しかし、プログラムは終了しません。つまり、プログラムは実行され続けますが、何も出力されません。誰か助けてもらえますか?

public static void readFile(String fileName, PhoneBook book_list)
{
    if(fileName == null) return;
    File file = new File(fileName);
    if(!file.isFile())
    {
        System.out.println("File not found!");
        return ;
    }

    Person person = new Person();
    boolean invalid = false;

    System.out.println("\nInfo: Beginning of Read-in records list.\n");

    try
    {
        BufferedReader input = new BufferedReader(new FileReader(file));
        try
        {
            String line = "";
            line = input.readLine();

            while(line != null)
            {
                line = line.trim();
                String[] words = line.split("\\s+");

                if(invalid)
                {
                    if(words[0].equals(""))
                    {
                        person = new Person();
                        invalid = false;
                    }
                }
                //refer to end of a record
                else if(words[0].equals(""))
                {
                    if(person.validation())
                    {
                        book_list.addPerson(person);
                        person = new Person();
                    }
                    else
                    {
                        person = new Person();
                    }
                }

                if(words[0].equalsIgnoreCase("name"))
                {
                    if(words.length<2)
                    {
                        invalid=true;
                    }
                    else
                    {
                        String name = words[1];
                        for(int i=2; i<words.length;i++)
                        {
                            name = name + " " + words[i];
                        }
                        for(int i=0; i<name.length(); i++)
                        {
                            if((name.codePointAt(i) >= 97 && name.codePointAt(i) <= 122)/*a-z*/
                                    || (name.codePointAt(i) >= 65 && name.codePointAt(i) <= 90)/*A-z*/
                                    || name.codePointAt(i) == 32)/*space*/
                            {continue;}

                            else
                            {
                                invalid=true;
                                break;
                            }
                        }
                        if(!invalid)
                        {
                            person.setName(name);
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("birthday"))
                {
                    if(words.length !=2)
                    {
                        invalid=true;
                    }

                    else
                    {
                        try
                        {
                            person.setBirthday(words[1]);
                            book_list.addPerson(person);
                        }
                        catch (ParseException e)
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("phone"))
                {
                    if(words.length != 2)
                    {
                        invalid = true;
                    }

                    else
                    {
                        String phone = Tools.parsePhone(words[1]);
                        if(phone!=null)
                        {
                            person.setPhone(phone);
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("email"))
                {
                    if(words.length != 2)
                    {
                        invalid = true;
                    }
                    else
                    {
                        if(Tools.validateEmail(words[1]))
                        {
                            person.setEmail(words[1]);
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("address"))
                {
                    String address = line.substring(words[0].length()).trim();

                    String addr="";

                    do
                    {
                        line = input.readLine();
                        if(line == null)
                        {
                            person.setAddress(address);                             
                        }
                        if(!invalid && person.validation())
                        {
                            book_list.addPerson(person);
                        }
                        addr = line.trim();
                        String[] adds = addr.split("\\s+");

                        if(!adds[0].equals("")
                                && !adds[0].equalsIgnoreCase("name")
                                && !adds[0].equalsIgnoreCase("birthday")
                                && !adds[0].equalsIgnoreCase("phone")
                                && !adds[0].equalsIgnoreCase("email")
                                && !adds[0].equalsIgnoreCase("address"))
                        {
                            address = address + " " + addr;
                        }

                        else break;
                    }
                    while(true);

                    if(line == null)
                        break;
                }

            }// end of while loop
        }

        finally
        {
            input.close();
        }
    }
    catch(IOException ex)
    {
        System.err.print("Error: Open records file failed");
        return;
    }
    System.out.println("\nInfo: End of Read-in records list.\n");
    return;
}


public static void main(String[] args) 
{
    String personFile = null; // person contact information file
    String instFile = null; //instruction file
    String outputFile = null; //output file name;
    String reportFile = null; //report file name;



    PhoneBook book_list = new PhoneBook();
    FileIO2.readFile("C:/Users/phoenix/Desktop/sample_phonebook1.txt",  book_list);

    ArrayList<Person> a;
    a = book_list.getPersonList();

    System.out.println(a.size());


}
4

3 に答える 3

3

あなたの問題はここにあると思います:

// initializes "line" String
String line = "";
// Tells BufferedReader to read one line (the first line)
line = input.readLine();
// loops infinitely since String read from first line is not null in this specific case
while(line != null)
{

...

これを試して:

// initializes "line" String
String line = "";
// tells the BufferedReader to read a new line _until_ the new line is null
// ... if new line is null we reached EOF
while ((line = input.readLine() != null) {

...

于 2013-05-11T16:39:54.987 に答える