2016-12-15 86 views
0

因此,我有一個任務是從文本文件中讀取動物的常用名稱和科學名稱,並用逗號分隔。 (即犬,犬紅斑狼瘡)。按姓名先按字母順序排序並打印到控制檯,然後按科學名稱按字母順序排序並打印到控制檯。我遇到的問題是要在文件中讀取並將它們放入數組中。對不起,如果這個代碼是可怕的錯誤,我仍然在學習。下面是竟然放棄了我的問題代碼:如何閱讀以逗號分隔的名稱列表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(); 
      } 
     } 
    } 
} 

} 
+0

你有什麼問題? – shmosel

+0

對不起,剛纔在代碼下面加了錯誤信息 – Algorn120

+0

你看的這個詞是「逗號分隔值」 –

回答

1

行讀取和解析看起來不錯,但您不在任何地方存儲list。在您的while循環之前,爲您的配對聲明一個容器。您可以使用Array,但由於它們必須按字母順序排序,因此最好將它們放入TreeSet<(String,String)>中,以便爲您排序。要打印出已排序的列表,請調用集合的iterator()方法來遍歷其值。

+0

添加了「String [] commonname = new String [25]; String [] scienname = new String [25];」,我從來沒有在類中使用TreeSet或迭代器,有沒有辦法使用它一個數組並將其排序在數組中? – Algorn120

+0

另外,我仍然遇到同樣的錯誤,告訴我我需要一個「(」已經有一個,並且我需要一個「;」我已經有一個。「任何線索是什麼原因造成的? – Algorn120

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[]後,我不再看到問題中發佈的問題。

編輯編輯

我沒有調試你的應用程序時,你有一些電話,我不能確定你的意圖。這是你的代碼,有一些改變,可以編譯。你可以從這裏觸及它。

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"); 
    } 
} 
+0

已將上述內容更改爲''code'「public static String readNames(String [] commonname,String [] scienname)throws IOException {」'code' – Algorn120

+0

已將上述更改爲'public static String readNames(String [] commonname,String [] scienname)throws IOException { '但我仍然得到相同的兩個錯誤 – Algorn120

+0

對不起,我不能編輯評論,我是新來的如何這個網站格式。我希望你明白我的意思 – Algorn120