[英]parallel arrays read from textfile
So i have this code so far and i want the scanner to read from a file and i want the second array to increment every time a word is repeated from the first array...but it messes up for the first category....any ideas? 所以到目前为止,我已经有了这段代码,并且我希望扫描仪从文件中读取数据,并且我希望第二个数组在每次从第一个数组中重复一个单词时都递增...但是对于第一个类别来说却很混乱。有任何想法吗?
Scanner sc=new Scanner(new File("Categories.txt"));
String category=sc.nextLine();
int catnum=sc.nextInt();
int count[]=new int[catnum];
ArrayList<String>anim=new ArrayList<String>();
while(sc.hasNextLine())
{
String temp=sc.nextLine();
anim.add(temp);
}
Collections.sort(anim);
System.out.println(anim);
for(int i=0;i<count.length;i++)
{
String x=anim.get(0);
for(int j=0;j<anim.size();j++)
{
if(anim.get(j).equals(x))
{
count[i]++;
anim.remove(j--);
}
}
}
for(int i=0;i<count.length;i++)
{
System.out.println(count[i]);
}
count.size()
returns 0 the first time since you never added anything to it, so you never enter in the loop. 由于您从未向其中添加任何内容,所以count.size()
首次返回0,因此您永远不会进入循环。 Use scategories.size()
instead. 请改用scategories.size()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.