目前我正在尝试编写一个可以从文件中获取多个名称并重新排列它们的程序 .
输入:
marry brady
harold mackenzie
karthik kanagasabathy
juri huddler
magic johnson
fred jones
tony ng
alfonzo camazinavichuk
herman goring
herbert milfons
annie li
samantha choorizoonanni
deb lee
然后,我想要在格式良好的单个框中输出它们 .
* * * * * * * *
* brady, m *
* * * * * * * *
* * * * * * * *
* harold, m *
* * * * * * * *
等等...
我试图通过创建一个 stripSpace() 方法,一个名为 formal 的方法来重新排列名称,最后 show 来在框中显示名称 .
我的代码未完成,我正在努力创建show方法,将名称放在格式良好的框中 .
我设法扭转这些名字,以便他们现在而不是说,george clooney他们现在是clooney,乔治,但我希望他们是clooney,g .
到目前为止这是我的代码:
import java.io.*;
public class MethodsAssignment {
public static void main(String[] args) throws IOException {
File file = new File("//Users//Desktop//names copy.txt");
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
String line;
String fixedWords;
String finalList;
line = reader.readLine();
while (line != null) {
fixedWords = stripSpace(line);
finalList = formal(fixedWords);
show(finalList);
line = reader.readLine();
}
}
public static String stripSpace(String name) {
String spaceless = name.trim();
return spaceless;
}
public static String formal(String name) {
String firstName = name.substring(0, name.indexOf(" ")); //write the frm 0 a la space
String lastName = name.substring(name.indexOf(" ") + 1);// from one after the space ONWARDS
String cName = lastName + ", " + firstName;
return cName;
}
public static void show (String list) {
}
}