因此,我有一个任务是从文本文件中读取动物的常用名称和科学名称,并用逗号分隔。 (即犬,犬红斑狼疮)。按姓名先按字母顺序排序并打印到控制台,然后按科学名称按字母顺序排序并打印到控制台。我遇到的问题是要在文件中读取并将它们放入数组中。对不起,如果这个代码是可怕的错误,我仍然在学习。下面是竟然放弃了我的问题代码:如何阅读以逗号分隔的名称列表Java
String[] commonname = new String[25];
String[] scienname = new String[25];
public static String readNames(String[] commonname, scienname) throws IOException {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader("/C:/Desktop/animals.txt"));
String line = null;
while ((line = inputStream.readLine()) != null) {
Scanner sc = new Scanner(line);
sc.useDelimiter(",");
String common = sc.next();
String scient = sc.next();
String list = new String(common, scient);
}
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
我得到2个错误,
"
File: C:\Users\Nathan\Desktop\Program5.java [line: 16]
Error: Syntax error on token "(", ; expected
File: C:\Users\Nathan\Desktop\Program5.java [line: 16]
Error: Syntax error, insert ";" to complete LocalVariableDeclarationStatement
"
我已经给了(在行它的询问,以及;不应该是尽量需要如我所知 这是非常不完整的,我很乐意帮助只读名称到一个字符串,既有通用名称也有科学名称,但可以按字母顺序排序,或者如果有意义的话。 这是完整的的代码如果流出任何光线:
/**
* Auto Generated Java Class.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Program5 {
String[] commonname = new String[25];
String[] scienname = new String[25];
public static void main(String[] args) throws IOException {
String list = readNames;
Arrays.sort(list);
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
public static String readNames(String[] commonname, String[] scienname) throws IOException {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader("/C:/users/Nathan/Desktop/animals.txt"));
String line = null;
String[] list = new String[25];
while ((line = inputStream.readLine()) != null) {
Scanner sc = new Scanner(line);
sc.useDelimiter(",");
String common = sc.next();
String scient = sc.next();
String list = new String(common, scient);
}
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
你有什么问题? – shmosel
对不起,刚才在代码下面加了错误信息 – Algorn120
你看的这个词是“逗号分隔值” –