My program is only reading the name of the file and none of the contents inside. I get an ElementDoesNotExist error every time. 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.
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. chooseFile
is a String, so you are calling Scanner(String)
when what you probably want is Scanner(File)
. Because of this your code does not read any files, it is reading the literal string "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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.