2 回答
TA贡献1865条经验 获得超7个赞
考虑在List此处使用:
List<String> famNum=new ArrayList();
while(inf.hasNext()){
family = inf.next();
famNum.add(family);
quan = i;
System.out.println(family);
i++;
}
TA贡献1810条经验 获得超5个赞
这是将计数器的变量附加到族变量末尾的示例:
FileReader read = new FileReader("PATH");
Scanner inf = new Scanner(read); //stands for in file
String family;
int quan;
int i = 1;
while(inf.hasNext()){
family = inf.next();
String famNum = family + " " + i; //create a variable with the int "i" appended to the end of the variable name to represent how many families there are.
quan = i;
System.out.println(famNum);
i++;
}
这是你的输出:
johnsons 1
flintstons 2
harrolds 3
smiths 4
cambells 5
但是,无需创建新对象。您可以真正附加如下:
family += " " + i;
只需使用 += 运算符 + i。
添加回答
举报