如何阅读以逗号分隔的名称列表Java
因此,我有一个任务是从文本文件中读取动物的常用名称和科学名称,并用逗号分隔。 (即犬,犬红斑狼疮)。按姓名先按字母顺序排序并打印到控制台,然后按科学名称按字母顺序排序并打印到控制台。我遇到的问题是要在文件中读取并将它们放入数组中。对不起,如果这个代码是可怕的错误,我仍然在学习。下面是竟然放弃了我的问题代码:如何阅读以逗号分隔的名称列表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();
}
}
}
}
}
行读取和解析看起来不错,但您不在任何地方存储list
。在您的while
循环之前,为您的配对声明一个容器。您可以使用Array
,但由于它们必须按字母顺序排序,因此最好将它们放入TreeSet<(String,String)>
中,以便为您排序。要打印出已排序的列表,请调用集合的iterator()
方法来遍历其值。
看来,如果你的问题是在构造函数中
readNames(String[] commonname, scienname)
您需要定义一个类型scienname
。
即
readNames(String[] commonname, String[] scienname)
第二误差,提通过这里提到的编译错误丢失;
,正在原因。
解决这两个问题,这是我的假设,两个错误都会消失。
编辑
粘贴到这一点我的IDE,并发现更多的问题
String list = new String(common, scient);
/\ the constructor for string takes many things, two String arrays is not one of them.
Available String
constructors这里。
而且
此方法必须返回字符串类型
你的方法从不返回一个值的结果,但它的返回类型是,String
。
在构造函数中添加缺少的String[]
后,我不再看到问题中发布的问题。
编辑编辑
我没有调试你的应用程序时,你有一些电话,我不能确定你的意图。这是你的代码,有一些改变,可以编译。你可以从这里触及它。
public class JTest
{
static String[] commonname = new String[25];
static String[] scienname = new String[25];
public static void main(String[] args)
throws IOException
{
// make a call to the rean names method with parameters
String list = readNames(commonname, scienname);
// not sure what you intend to sort, you can not sort a string
// 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();
// not sure what your intention is here
//String list = new String(common, scient);
}
}
finally
{
if (inputStream != null)
{
inputStream.close();
}
}
// return a String, as specified in method definition
return new String("with some content");
}
}
已将上述内容更改为''code'“public static String readNames(String [] commonname,String [] scienname)throws IOException {”'code' – Algorn120
已将上述更改为'public static String readNames(String [] commonname,String [] scienname)throws IOException { '但我仍然得到相同的两个错误 – Algorn120
对不起,我不能编辑评论,我是新来的如何这个网站格式。我希望你明白我的意思 – Algorn120
你有什么问题? – shmosel
对不起,刚才在代码下面加了错误信息 – Algorn120
你看的这个词是“逗号分隔值” –