如何读取逗号Java分隔的名称列表

时间:2016-12-15 21:02:24

标签: java arrays string file sorting

所以我有一项任务,用文字文件读取动物的通用名称和科学名称,用逗号分隔。 (即狗,犬红斑狼疮)。按通用名称的字母顺序对它们进行排序,然后打印到控制台,然后按科学名称按字母顺序排序并打印到控制台。我遇到了一个类来读取文件并将它们放入数组中的问题。对不起,如果这段代码非常错误,我还在学习。这是给我问题的代码:

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
" 

我已经给了它(在它要求的行中,并且;据我所知,不应该需要。 这是非常不完整的,我喜欢帮助只是将名称读成具有Common和Scientific名称的字符串,但可以按字母顺序排序,或者如果有意义的话。 如果有任何明白的话,这就是整个代码:

/**
 * 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();
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

行读取和解析看起来很好,但您并未将list存储在任何地方。在while循环之前,为您的对声明一个容器。你可以使用Array,但由于它们必须按字母顺序排序,你也可以将它们扔进TreeSet<(String,String)>,它会为你排序。要打印出已排序的列表,请调用set的iterator()方法以循环显示其值。

答案 1 :(得分:0)

好像你的问题在构造函数中

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

在构造函数中添加缺少的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");
    }
}