[英]Splitting a String by Comma, then put it in a Treemap
我有一個名為“ marathon”的文件,其中有7個鍵:
以逗號“,”分隔。 我必須將第二個鍵(時間)放在樹形圖中。 目前,我只想顯示控制台中的時間。
所以這是我的代碼:
public class Text {
@SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader in = new BufferedReader(new FileReader("marathon"));
String str;
str = in.readLine();
while ((str = in.readLine()) != null) {
//System.out.println(str);
String[] ar=str.split(",");
System.out.println(ar[0]);
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
}
這是一行文本的樣子:
M,2:30:57.6,Harry Payne,GBR,1929-07-05,英國斯坦福橋
當我啟動代碼示例的程序並將其放入System.out.println(ar[0]);
a[0]
向我顯示了控制台的第一行,因此是M和F。 但是當我放置a[1]
有一個例外:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
正如其他人指出的那樣,在進入循環主體之前,您需要兩次執行readline,因此您將錯過第一行。
但是您也沒有檢查readline是否導致格式正確的行。 它可能是空行或以其他方式未生成您期望的數組的行。
因此,您應該添加一個if語句,以檢查您的期望值,例如:
public class Text {
@SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader in = new BufferedReader(new FileReader("marathon"));
String str = "";
while ((str = in.readLine()) != null) {
String[] ar=str.split(",");
if(ar.length >= 7) {
System.out.println(ar[0] + ", " + ar[1]);
}
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
}
請嘗試下面的代碼。 它為我工作。
您應該只在while循環中讀取過該行一次。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Text {
@SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader in = new BufferedReader(new FileReader("marathon"));
String str;
while ((str = in.readLine()) != null) {
//System.out.println(str);
String[] ar=str.split(",");
System.out.println(ar[0]);
System.out.println(ar[1]);
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
}
SortedMap<String, String[]> map = new TreeMap<>();
Path path = Paths.get("marathon");
Files.lines(path, Charsets.defaultCharset())
.map(line -> line.split(",\\s*"))
.peek(words -> {
if (words.length != 7) {
Logger.getLogger(getClass().getName()).info("Line wrong: " + line);
}
})
.filter(words -> words.length == 7)
.forEach(words -> map.put(word[1], words));
但是,那里有CSV閱讀器類,可以處理帶引號等的引號字段。
Java 8只是為了好玩
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Stream;
import static java.nio.charset.Charset.defaultCharset;
import static java.lang.System.out;
import static java.nio.file.Files.lines;
public class Main {
public static void main(String[] args) throws IOException {
Map<String, String[]> map = new TreeMap<>( );
try( Stream<String> lines = lines(Paths.get("marathon"), defaultCharset())){
lines.map(line -> line.split( "," )).forEach( entry -> map.put(entry[1], entry ));
map.values().forEach( entry -> out.println(Arrays.toString( entry )) );
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.