I have a file and it contains name, surname and age information. For example: Mike, Tyson, 54. There is 1 person in every 1 row. I just want to read the names. How can I do that? I did the reading of all lines, but I could not only read the name.
public static void main(String[] args) {
File f = new File("C:/Users/muham/Desktop/students.txt");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String s = reader.readLine();
String[] students = new String[6];
int i=0;
while(s != null){
students[i] = s;
s=reader.readLine();
i++;
}
Arrays.sort(students);
String[] arr = null;
for(i = 0; i<students.length;i++){
System.out.println(students[i]);
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
}
Use split to get the name try this code
public static void main(String[] args) {
File f = new File("C:/Users/muham/Desktop/students.txt");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String s = reader.readLine();
String[] students = new String[6];
int i=0;
while(s != null){
students[i] = s.split(",")[0];
s=reader.readLine();
i++;
}
//Arrays.sort(students);
String[] arr = null;
for(i = 0; i<students.length;i++){
System.out.println(students[i]);
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.