2017-01-30 56 views
2

輸入文件具有這樣的數據:和鋼帶的其餘部分,爪哇

1 // Comments 0 
0 
14 // Comment 1 
4 // Comment 12 
32 
21 // Comment 13 

我需要只取那些第一整數,如1,0,14等並把它們放到它們自己的整型數組中。我試圖使用Scanner,但使用正則表達式split("\\s")似乎並沒有做什麼,我需要。

我的想法是把它全部帶入String arraylist,我已經做了,但在那一點上,我不知道如何去掉那些整數。這個問題的很大一部分是每個評論也有整數在裏面,所以如果我只是得到了所有的整數在每一行,這些都是還包括...我不想。

如果有人可以幫助我通過邏輯我需要處理這個文件,我將不勝感激。謝謝。

+0

做你的線條始終只包含一個整數? – Aaron

+1

你可以顯示你的代碼到目前爲止,並且你是一行一行讀取文件還是整個文件? –

回答

4

使用Scanner是一個好主意,但split不是。

假設每個相關的行開始一個整數,不包含其他相關信息,並要停止,一旦你遇到不尊重這個格式的線,你可以使用下面的代碼:

Scanner s = new Scanner(inputFile); 
List<Integer> extractedIntegers = new ArrayList<>(); 
while (s.hasNextInt()) { // check if the line starts with an integer 
    extractedIntegers.add(s.nextInt()); 
    s.nextLine(); //consumes the rest of the line in order to skip over the comments 
} 
s.close(); 
+0

偉大的解決方案。結果發現有一件我需要處理。在文件末尾附近,列出了一個空白行,然後是一個數字「.1000」,我還需要捕獲它。假設我使用String arrayList,我該如何處理? –

+0

@BrianJay是作爲標記重要的空行,還是足以搜索匹配'^ \。\ d + $'的行? – Aaron

+0

不,空行作爲標記並不重要。具體來說,當我在「.1000」中讀取時,我需要將隨後的整數放入整數數組的不同部分。所以,「.1000」是重要的。 –

0

我想你可以用下面這個片段。

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

public class ReadFileExample2 { 
    private static final String FILENAME = "file path"; 
    public static void main(String[] args) { 

     try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) { 
      String sCurrentLine; 
      while ((sCurrentLine = br.readLine()) != null) { 
       //TODO split the sCurrentLine with "//s"(space) if there is 
       //a space after integer in each row and read first element from the array 
       //and validate the first element with integer regex('^\d+$') to validate 
       //if that is an integer or not 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
0

這裏是一個java 8答案:

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Class { 
    private final static Pattern p = Pattern.compile("^([\\d.-]+)"); 

    public static void main(String... args) throws IOException { 
    final Path path = Paths.get("/path/to/your/file.txt"); 
    Files.lines(path) 
     .map(p::matcher) 
     .filter(Matcher::matches) 
     .map(m->m.group(1)) 
     .forEach(System.out::println); 
    } 
} 
0

使用FilesStream API和一個regex pattern matcher的變體。

final Pattern STARTS_WITH_NUMBER_PATTERN = Pattern.compile("^([\\d.]+)"); 
Files.lines(Paths.get(/* the path to your file */)) 
    .map(STARTS_WITH_NUMBER_PATTERN::matcher) 
    .filter(Matcher::find) 
    .map(Matcher::group) 
    .collect(Collectors.toList()); 

或省略的最後一行,只是打印出來的值用forEach(System.out::println);

如果你只是有一個ArrayList<String>你可以做基本相同。 yourArrayList.stream():只是交換Files.lines...直插式。

請注意,您也可以使用Scanner來做到這一點。對於一些空行後面的數字解析,您可能需要使用一些附加條件。另一方面的流解決方案可能會變得更具可讀性。

0

import java.io.BufferedReader;

import java.io.IOException;

進口java.io.的FileReader;

公共類ReadOnlufirstIntegerInFile {

private static void checkFirstInteger(String line) { 

    char[] words = line.trim().toCharArray(); 
    for(int i = 0; i < words.length; i++) { 

     if(words[i] >= 48 && words[i] <= 57) { 
     System.out.println(words[i]); 
     break; 
    } 
    } 
} 

public static void main(String[] args) { 

    FileReader fileReader = null ; 
    BufferedReader bufferedReader = null ; 
    try { 

     fileReader = new FileReader("F:\\VIKU\\a.txt"); 
     bufferedReader = new BufferedReader(fileReader); 
     String line = bufferedReader.readLine(); 

     while(line != null) { 
     // System.out.println(line); 
     checkFirstInteger(line); 
     line = bufferedReader.readLine(); 
     } 
    } catch (IOException e) { 
     System.out.println(e); 

    } 

    finally() { 
    try { 
      bufferedReader.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    try { 

     fileReader.close(); 
    } catch (Exception e2) { 
     e2.printStackTrace(); 
    } 
    } 

} 

}