2017-11-08 45 views
0

ファイルから読み込んで配列に追加しようとすると、完全に困惑します。そこで私が実装しようとしたのは、一時的なString配列に行を読み込んでから、メイン配列に一時的な値を追加することでした。ファイルから配列を読み込む

これは、行ごとに配列に分割したいテキストファイルの内容です。私は各行を取ることができるように、数値で計算し、出力をフォーマットします。しかし

Mildred Bush 45 65 45 67 65 into [[Mildred Bush],[45],[65],[67],[65]] 
    Fred Snooks 23 43 54 23 76    etc. 
    Morvern Callar 65 45 34 87 76   etc. 
    Colin Powell 34 54 99 67 87 
    Tony Blair 67 76 54 22 12 
    Peter Gregor 99 99 99 99 99 

、メインアレイでいただきまし実行[ミルドレッド、フレッド、Morvern、コリン、トニー、ピーター]です。つまり、最初の値だけがメイン配列に追加され、私のコードでこれをどのように修正するのか分かりません。

//開くファイルの名前。

String fileName = "Details.txt"; 

    String wfilename = "output.txt"; 

    // This will reference one line at a time 
    String line = null; 

    String temp; 

    try { 
     // FileReader reads text files in the default encoding. 
     FileReader fileReader = new FileReader(fileName); 

     FileWriter fileWriter = new FileWriter(wfilename); 


     BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 

     // Always wrap FileReader in BufferedReader. 
     BufferedReader bufferedReader = new BufferedReader(fileReader); 

     List<String> parts = new ArrayList<>(); 
     String [] temp2; 

     while((line = bufferedReader.readLine()) != null) { 
      //System.out.println(line); 
      temp = line; 
      temp2 = line.split(" "); 

      parts.add(temp2[0]); 
      //System.out.println(line); 
      bufferedWriter.write(line + "\n"); 
     } 
     System.out.print(parts); 

     // Always close files. 
     bufferedReader.close(); 
     bufferedWriter.close(); 
    } 
    catch(FileNotFoundException ex) { 
     System.out.println("Unable to open file '" + fileName + "'");     
    } 
    catch(IOException ex) { 
     System.out.println("Error reading file '" + fileName + "'");     

    } 

更新の試み:私は、私はエラーが

であると当たった

parts.add(temp2) 

をしようとしたとき、この理由は

parts.add(temp2[0]); 

まだだったことが判明

The method add(String) in the type List<String> is not applicable for the arguments (String[]) 

基本的には、私が苦労しているのは配列に配列を追加することです。

EDIT2:

それはつのアレイにファイル内のすべてのアイテムが追加されたことに働い

for(int i=0; i<7; i++){ 
       parts.add(temp2[i]); 
      } 

を試みました。リストを7つの用語ごとに分割して2D配列にすることができる方法があれば疑問に思っていましたか?

これは必須ではありませんが、計算のためにはforループを使用し、各行の計算を行うときは[i + 7]を実行するのが悪い習慣であると感じます。

+1

[デバッガ](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help)でコードをステップ実行しようとしましたか? - 私 - 診断 - 問題)? – litelite

+0

[doc](https://docs.oracle.com/javase/7/docs/api/java/util/List.html)をご覧ください。配列に配列を追加できるものがあります最初のいくつかの方法;) – litelite

+1

あなたの分割に問題があります。 [0]は最初の単語だけを返すので、2つの単語も分割します。 「Mildred Bush」は、あなたが望んでいないおそれのある「Mildred」のみを提供します – logger

答えて

0

このお試しください:私は同様の問題があったと私はこれを見つけた...私は私が助けてきました願っています

 public static boolean isNumeric(String str){ 
      try{ 
      double d = Double.parseDouble(str); 
      } 
      catch(NumberFormatException nfe){ 
      return false; 
      } 
      return true; 
     } 

0

:ここ

  try{ 
       br = new BufferedReader(new FileReader("myfile.txt")); 

       //Save line by line the file in ArrayList 
       while((contentLine = br.readLine()) != null){    
        arrlist.add(contentLine); 
       } 

       //Iterate the list and applies Split to get the data 
       for(int i = 0; i < arrlist.size(); i++){ 
        String[] splitString = arrlist.get(i).split(" "); 
         for(int j = 0; j < splitString.length; j++){ 
          if(isNumeric(splitString[j])){ 
           System.out.print(splitString[j]+"*"); 
          } 
         } 
         System.out.println(); 
       } 

      }catch(IOException io){ 
       io.printStackTrace(); 
      } 

メソッドがIsNumeric関数です多次元配列のソリューション:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Arrays; 

public class Test { 

    //first I read the file into a String 
    static String readFile(String fileName) throws IOException { 
     BufferedReader br; 
      br = new BufferedReader(new FileReader(fileName)); 
     try { 
      StringBuilder sb = new StringBuilder(); 
      String line = br.readLine(); 

      while (line != null) { 
       sb.append(line); 
       line = br.readLine(); 
       } 
      return sb.toString(); 
     } finally { 
      br.close(); 
     } 
    } 

    public static void main (String [] args) { 
     String input = new String(); 
     try { 
      input = readFile("Example.txt"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     //Then I convert it 
     String dimension1 = "\\|", dimension2 = " ", dimension3 = ","; //That could also be other characters 

     String [] arrayString1D = input.split(dimension1); 
     String [][] arrayString2D = new String[arrayString1D.length][]; 
     String [][][] arrayString3D = new String[arrayString2D.length][][]; 

     for(int x = 0; x < arrayString1D.length; x++) { 
      arrayString2D[x] = arrayString1D[x].split(dimension2); 
      arrayString3D[x] = new String [arrayString2D[x].length][]; 

      for(int y = 0; y < arrayString2D[x].length; y++) { 
       arrayString3D[x][y] = arrayString2D[x][y].split(dimension3); 
      } 
     } 

    //And here I print it out! 
    System.out.println(Arrays.deepToString(arrayString3D)); 
    } 
} 

テキストのテキスト。ファイルには、例えば次のようになります。
word1,word2,word3,word4,word5|word6,word7 word8|word9,word10,word11 word12,word13

、それはこの印刷になります。私はそれが役に立てば幸い
[[[word1, word2, word3, word4, word5]], [[word6, word7], [word8]], [[word9, word10, word11], [word12, word13]]]

を!:-)

関連する問題