I have made an ArrayList.txt which contains a list of students with their grades, a separate class student containing getName()
, getGrade()
, getMarks()
etc and on my main class, I have a main method which will pull from this method
我创建了一个ArrayList.txt,其中包含学生的成绩列表,一个单独的班级学生,包含getName(),getGrade(),getMarks()等,在我的主课上,我有一个主要的方法,将从中拉出来方法
Hence my method goes as this:
因此我的方法如下:
public static String findName(ArrayList<Student> o, String name) {
for(int i = 0; i < o.size(); i++)
{
if(o.get(i).getName().startsWith(name.toLowerCase()))
System.out.print(o.get(i));
}
return name;
}
and my main method has this findName(students, "al");
我的主要方法有这个findName(学生,“al”);
my array.txt looks something like this
我的array.txt看起来像这样
afafwafa
B
80
alicegg
C
70
lfhif
D
50
Allllleeee
A
94
however my findName()
method is unable to pull all names that starts with "al" even though all my other methods are working. Also I need to take note that the method should be case insensitive and i need to use .startsWith()
. Any suggestions?
但是我的findName()方法无法拉出以“al”开头的所有名称,即使我的所有其他方法都正常工作。另外我需要注意该方法应该不区分大小写,我需要使用.startsWith()。有什么建议?
To add on, with the given ArrayList of type student with a string file name, i need to create a method that will write the members of the arraylist to a text file with a given name with 1 element to a line using student class toString method format. i need to handle possible exceptions with try/catch. I also need to use either filereader, fileoutputsteam, printwriter, printstream in the code
要添加,使用给定的带有字符串文件名的student类型的ArrayList,我需要创建一个方法,使用student类toString方法将arraylist的成员写入具有给定名称的文本文件,其中1个元素到一行格式。我需要使用try / catch处理可能的异常。我还需要在代码中使用filereader,fileoutputsteam,printwriter,printstream
public static void nameToFile(ArrayList<Student> a, String file) {
int i = inone.read();
FileReader inone = new FileReader(a.get(i).toString());
FileOutputStream fos = new FileOutputStream(file);
PrintWriter outone = new PrintWriter(fos);
outone.close();
}
my main method has this
我的主要方法有这个
nameToFile(students2,"modified.txt");
1 个解决方案
#1
2
Also i need to take note that the method should be case insensitive and i need to use .startsWith().
另外我需要注意该方法应该不区分大小写,我需要使用.startsWith()。
If you want your startsWith()
method to ignore the case, just convert both to lower or upper case before comparison:
如果你想让你的startsWith()方法忽略这种情况,只需在比较之前将它们转换为小写或大写:
if(o.get(i).getName().toLowerCase().startsWith(name.toLowerCase()))
If your findName()
is supposed to just return 1 name, you can return it in the if-condition. If you want to return a list of names, add it to another arraylist, then return the arraylist.
如果你的findName()只返回1个名字,你可以在if条件中返回它。如果要返回名称列表,请将其添加到另一个arraylist,然后返回arraylist。
#1
2
Also i need to take note that the method should be case insensitive and i need to use .startsWith().
另外我需要注意该方法应该不区分大小写,我需要使用.startsWith()。
If you want your startsWith()
method to ignore the case, just convert both to lower or upper case before comparison:
如果你想让你的startsWith()方法忽略这种情况,只需在比较之前将它们转换为小写或大写:
if(o.get(i).getName().toLowerCase().startsWith(name.toLowerCase()))
If your findName()
is supposed to just return 1 name, you can return it in the if-condition. If you want to return a list of names, add it to another arraylist, then return the arraylist.
如果你的findName()只返回1个名字,你可以在if条件中返回它。如果要返回名称列表,请将其添加到另一个arraylist,然后返回arraylist。