只有第一个项目在从ArrayList读取CSV文件时丢失。

huangapple 未分类评论44阅读模式
标题翻译

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&lt;T&gt;{
	ArrayList&lt;String&gt; firstLine = new ArrayList&lt;&gt;();
	ArrayList&lt;ArrayList&lt;T&gt;&gt; veri = new ArrayList&lt;ArrayList&lt;T&gt;&gt;();
	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(&quot;;&quot;);
			while(lineT.hasNext()) {
				String StringObj = lineT.next();
				firstLine.add(StringObj);
			}
			while(in.hasNextLine()) {
				ArrayList&lt;T&gt; temp = new ArrayList&lt;&gt;();
				lineT = new Scanner(in.nextLine()).useDelimiter(&quot;;&quot;);
				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&lt;ArrayList&lt;T&gt;&gt;();
			System.out.println(&quot;Dosya ile ilgili bir hata olustu.&quot;);
		}
		catch(IOException e) {
			System.out.println(&quot;Dosya ile ilgili bir hata olustu.&quot;);
		}
	}
	
	public Database join(Database second, String column_name){
		Database newObj = new Database();
		for(int i=0; i&lt;firstLine.size(); i++)
		System.out.print(firstLine.get(i));
		int firstIndex = firstLine.indexOf(column_name);
		int secondIndex = second.firstLine.indexOf(column_name);
	}
}

huangapple
  • 本文由 发表于 2020年3月16日 21:36:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/60707020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定