标题翻译
Only the first item is missed from ArrayList while reading from csv file
问题
我已经尝试从CSV文件中读取字符串并将其存储到ArrayList
中。但是,当我在ArrayList
中搜索第一个项的索引时,它返回-1。或者当我尝试检查给定的字符串和ArrayList
的第一个元素是否相等时,equals
方法不起作用。
以下是我的代码:
class Database<T>{
ArrayList<String> firstLine = new ArrayList<>();
ArrayList<ArrayList<T>> veri = new ArrayList<ArrayList<T>>();
public Database(){}
public Database(String dosyaIsmi) {
// ...(代码略)
}
public Database join(Database second, String column_name){
Database newObj = new Database();
for(int i=0; i<firstLine.size(); i++)
System.out.print(firstLine.get(i));
int firstIndex = firstLine.indexOf(column_name);
int secondIndex = second.firstLine.indexOf(column_name);
// ...(代码略)
}
}
请注意,我只对代码进行了翻译,去掉了您在代码中提到的问题描述部分。如果您有关于代码的进一步问题或需要解释,欢迎随时提问。
英文翻译
I have tried to read String from a CSV file and store it to the ArrayList
. But when searching the index of the first item in the ArrayList
, it returns -1. Or when I tried to check whether a given string and the first element of the ArrayList
is equal, equals method does not work.
Here is my code:
class Database<T>{
ArrayList<String> firstLine = new ArrayList<>();
ArrayList<ArrayList<T>> veri = new ArrayList<ArrayList<T>>();
public Database(){}
public Database(String dosyaIsmi) {
Scanner in = null;
Scanner lineT = null;
try {
in = new Scanner(new File(dosyaIsmi));
lineT = new Scanner(in.nextLine()).useDelimiter(";");
while(lineT.hasNext()) {
String StringObj = lineT.next();
firstLine.add(StringObj);
}
while(in.hasNextLine()) {
ArrayList<T> temp = new ArrayList<>();
lineT = new Scanner(in.nextLine()).useDelimiter(";");
if(lineT.hasNextInt()) {
while(lineT.hasNext()) {
if(lineT.hasNextInt()) {
Integer intObj = lineT.nextInt();
temp.add((T) intObj);
}
else
throw new FileNotFoundException();
}
}
else if(lineT.hasNextDouble()) {
while(lineT.hasNext()) {
if(lineT.hasNextDouble()) {
Double doubleObj = lineT.nextDouble();
temp.add((T) doubleObj);
}
else
throw new FileNotFoundException();
}
}
else {
while(lineT.hasNext()) {
if(lineT.hasNextInt())
throw new FileNotFoundException();
else if(lineT.hasNextDouble())
throw new FileNotFoundException();
else {
String stringObj = lineT.next();
temp.add((T) stringObj);
}
}
}
veri.add(temp);
}
in.close();
lineT.close();
}
catch(FileNotFoundException e) {
veri = new ArrayList<ArrayList<T>>();
System.out.println("Dosya ile ilgili bir hata olustu.");
}
catch(IOException e) {
System.out.println("Dosya ile ilgili bir hata olustu.");
}
}
public Database join(Database second, String column_name){
Database newObj = new Database();
for(int i=0; i<firstLine.size(); i++)
System.out.print(firstLine.get(i));
int firstIndex = firstLine.indexOf(column_name);
int secondIndex = second.firstLine.indexOf(column_name);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论