简体   繁体   English

程序只读取文件的标题,没有别的

[英]Program only reading title of file and nothing else

My program is only reading the name of the file and none of the contents inside.我的程序只读取文件名而不读取其中的任何内容。 I get an ElementDoesNotExist error every time.我每次都会收到 ElementDoesNotExist 错误。 The file is in the same folder, and I have no idea why it won't read the contents.该文件在同一个文件夹中,我不知道为什么它不会读取内容。 I understand that this program is inefficient, and it's my first time using File IO in Java.我知道这个程序效率低下,这是我第一次在 Java 中使用文件 IO。

import java.util.*;
    import java.io.*;
    
    public class Project4
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the year: ");
            int fileYear = input.nextInt();
            String userFile = "";
            if (fileYear == 2001)
            {
                userFile = "BabyNames2001.txt";
            }
    
            else if (fileYear == 2002)
            {
                userFile = "BabyNames2002.txt";
            }
    
            else if (fileYear == 2003)
            {
                userFile = "BabyNames2003.txt";
            }
    
            else if (fileYear == 2004)
            {
                userFile = "BabyNames2004.txt";
            }
    
            else if (fileYear == 2005)
            {
                userFile = "BabyNames2005.txt";
            }
    
            else if (fileYear == 2006)
            {
                userFile = "BabyNames2006.txt";
            }
    
            else if (fileYear == 2007)
            {
                userFile = "BabyNames2007.txt";
            }
    
            else if (fileYear == 2008)
            {
                userFile = "BabyNames2008.txt";
            }
    
            else if (fileYear == 2009)
            {
                userFile = "BabyNames2009.txt";
            }
    
            else if (fileYear == 2010)
            {
                userFile = "BabyNames2010.txt";
            } 
            File theFile = new File(userFile);
    
            ArrayList<BabyName> theBabies = loadNames("BabyNames2001.txt", fileYear);
    
            System.out.print("Enter the gender: ");
            String babyGender = input.next();
    
            System.out.print("Enter the name: ");
            String babyName = input.next();
    
            findName(babyName, fileYear, theBabies);
        }
    
        private static ArrayList<BabyName> loadNames(String fileName, int checkYear)
        {
            ArrayList<BabyName> babies = new ArrayList<BabyName>();
            int currentYear = checkYear;
            String chooseFile = fileName;
            Scanner reader = new Scanner(chooseFile);
            while (reader.hasNext())
            {
                BabyName boy = new BabyName();
                BabyName girl = new BabyName();
                boy.setYear(currentYear);
                girl.setYear(currentYear);
                boy.setGender("M");
                girl.setGender("F");
                String currentRank = reader.next();
                boy.setRank(currentRank);
                String boyName = reader.next();
                int boyTotal = reader.nextInt();
                String girlName = reader.next();
                int girlTotal = reader.nextInt();
                girl.setRank(currentRank);
                boy.setName(boyName);
                girl.setName(girlName);
                boy.setTotal(boyTotal);
                girl.setTotal(girlTotal);
                babies.add(boy);
                babies.add(girl);
                
            }
            return babies;
        }
    
        private static BabyName findName(String name, int year, ArrayList<BabyName> babies)
        {
            ArrayList<BabyName> theBabies = babies;
            BabyName tempBaby = new BabyName();
            String findName = name;
            int findYear = year;
            for (int baby = 0; baby < 2000; baby++)
            {
                if (findName == theBabies.get(baby).getName())
                {
                    tempBaby = theBabies.get(baby);
                }
            }
            return tempBaby;
        }
    }
    
    class BabyName
    {
        private String rank;
        private int year;
        private String name;
        private String gender;
        private int total;
    
        BabyName()
        {
        }
    
        public String getRank()
        {
            return rank;
        }
    
        public int getYear()
        {
            return year;
        }
    
        public String getName()
        {
            return name;
        }
    
        public String getGender()
        {
            return gender;
        }
    
        public int getTotal()
        {
            return total;
        }
    
        public void setRank(String newRank)
        {
            this.rank = newRank;
        }
    
        public void setYear(int newYear)
        {
            this.year = newYear;
        }
    
        public void setName(String newName)
        {
            this.name = newName;
        }
    
        public void setGender(String newGender)
        {
            this.gender = newGender;
        }
    
        public void setTotal(int newTotal)
        {
            this.total = newTotal;
        }
    }

Your problem is here:你的问题在这里:

Scanner reader = new Scanner(chooseFile);

The Scanner class has multiple overloaded constructors. Scanner class 有多个重载的构造函数。 chooseFile is a String, so you are calling Scanner(String) when what you probably want is Scanner(File) . chooseFile是一个字符串,所以当您可能想要的是Scanner(File)时,您正在调用Scanner(String) ) 。 Because of this your code does not read any files, it is reading the literal string "BabyNames2001.txt" .因此,您的代码不会读取任何文件,它正在读取文字字符串"BabyNames2001.txt"

The solution is to simply pass a File instead of a String, or something along the lines of:解决方案是简单地传递一个文件而不是一个字符串,或者像下面这样的东西:

Scanner reader = new Scanner(new File(chooseFile));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM