Java 无法从用户输入检索数组列表项
因此,我试图根据用户输入的状态缩写检索存储在arraylist中的值。arraylist包含州缩写、州名称和人口。所有这些项目都由csv文件使用循环读取到arraylist中。任何帮助都将不胜感激Java 无法从用户输入检索数组列表项,java,arraylist,Java,Arraylist,因此,我试图根据用户输入的状态缩写检索存储在arraylist中的值。arraylist包含州缩写、州名称和人口。所有这些项目都由csv文件使用循环读取到arraylist中。任何帮助都将不胜感激 public class Prog1 { public static void main(String[] args) { Scanner inFile = null; ArrayList<State> list = new ArrayList&l
public class Prog1 {
public static void main(String[] args) {
Scanner inFile = null;
ArrayList<State> list = new ArrayList<State>();
try {
inFile = new Scanner(new File( "states.txt" ) );
}
catch( FileNotFoundException e) {
System.err.println("Error: File states.txt not found");
}
State s;
String abb;
String name;
double population;
while(inFile.hasNext()) {
String file = inFile.nextLine();
String[] tokens = file.split( ",[ ]*" );
abb = tokens[0];
name = tokens[1];
population = Double.parseDouble( tokens[2] );
s = new State(abb, name, population);
list.add(s);
}
System.out.printf( "%-13s %-13s %3s\n", "Abbreviation", "Name", "Population" );
System.out.printf( "%-13s %-14s %3s\n", "----", "------", "---------", "\n" );
for( int i = 0; i < list.size(); i++ )
{
s = list.get(i);
System.out.printf( "%-13s %-15s %,2.0f\n",
s.getAbb(), s.getName(), s.getPopulation() );
}
System.out.println("Enter State Abbreviation:");
Scanner scan = new Scanner(System.in);
String userState = scan.next();
State search = new State(userState);
int index = list.indexOf( search );
if( index >= 0) {
State located = list.get(index);
System.out.printf( userState, located.getName(), located.getPopulation());
}
else {
System.out.println(userState + " not found");
}
}
}
public class State {
private String abb;
private String name;
private double population;
public State(String abb) {
super();
this.abb = abb;
}
public State(String abb, String name, double population) {
super();
this.abb = abb;
this.name = name;
this.population = population;
}
public String getAbb() {
return abb;
}
public String getName() {
return name;
}
public double getPopulation() {
return population;
}
@Override
public boolean equals(Object arg)
{
return this.abb.equals( ((State)arg).name );
}
}
公共类程序1{
公共静态void main(字符串[]args){
扫描仪填充=空;
ArrayList=新建ArrayList();
试一试{
infle=新扫描仪(新文件(“states.txt”);
}
catch(filenotfounde异常){
System.err.println(“错误:未找到文件states.txt”);
}
s国;
弦abb;
字符串名;
双种群;
while(infle.hasNext()){
String file=infle.nextLine();
字符串[]标记=file.split(“,[]*”);
abb=代币[0];
名称=代币[1];
population=Double.parseDouble(令牌[2]);
s=新州(abb、姓名、人口);
列表。添加(s);
}
System.out.printf(“%-13s%-13s%3s\n”、“缩写”、“名称”、“总体”);
System.out.printf(“%-13s%-14s%3s\n”,“-----------”,“-----------”,“-----------”,“\n”);
对于(int i=0;i=0){
State located=list.get(索引);
System.out.printf(userState,located.getName(),located.getPopulation());
}
否则{
System.out.println(userState+“未找到”);
}
}
}
公共阶级国家{
私人字符串abb;
私有字符串名称;
私人双重人口;
公共状态(字符串abb){
超级();
this.abb=abb;
}
公共状态(字符串abb、字符串名称、双重填充){
超级();
this.abb=abb;
this.name=名称;
这个。人口=人口;
}
公共字符串getAbb(){
返回abb;
}
公共字符串getName(){
返回名称;
}
公共人口{
返回人口;
}
@凌驾
公共布尔等于(对象arg)
{
返回此.abb.equals(((State)arg.name);
}
}
根据您想要过滤项目的条件是通过abbr
,因此我认为您应该更改:
return this.abb.equals(((State)arg).name);
借
状态
没有超类。为什么在构造函数中调用super()
?@AnilM每个类在不扩展到其他类时都隐式地是Object的子类。
return this.abb.equals(((State)arg).abbr);