如何使用掃描儀取出奇數行並按順序放置它們?如何使用掃描儀取出奇數行並按順序放置它們?
的問題是,我有一個文本文件,看起來像這樣:
case0:
a1:
0
a2:
0
a3:
0
a4:
0
a5:
0
a6:
0
a7:
1
a8:
0
a9:
0
我需要讓輸出如:
caseNumber :0
case content :000000100
怎麼辦呢?
這裏是一個版本,香港專業教育學院寫到目前爲止,你可能想添加一些對:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileScanner {
File f = new File("test.txt");
public static void main(String[] args) {
FileScanner fs = new FileScanner();
fs.readfile();
}
public void readfile() {
try {
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(f);
int lineCount = 0;
while (scanner.hasNextLine()) {
lineCount++;
String line = scanner.nextLine();
// System.out.println("Line :" + line);
String line1 = line.trim();
// System.out.println(line1);
for (lineCount = 0; lineCount<19; lineCount++) {
line1 = line1.substring(line1.lastIndexOf("e") + 1);
// System.out.println("what is here :"+line1);
line1 = line1.substring(0, line1.lastIndexOf(":"));
int caseNum = Integer.parseInt(line1);
System.out.println("caseNumber :" + caseNum);
};
// System.out.println(""+line1);
// int caseNumber = Integer.parseInt(line1);
// System.out.println("caseNumber :" + caseNumber);
}
System.out.println("line count" + lineCount);
} catch (FileNotFoundException e) {
}
// find the next token and print it
// System.out.println("" + scanner.next());
// find the next token and print it
// System.out.println("" + scanner.next());
// close the scanner
}
}
Contious最後一個問題.. 權,我得到這樣的輸出:
Case Number: -1
Case Contents: 000000100
Line Count: 4085
我認爲案件號碼有點不對。
然後,我就215套的情況下(在同一文件和相同的格式與第一組數據),我需要做的輸出,如:
Case Number: 0
Case Contents: 000000100
Case Number: 1
Case Contents: 000000101
Case Number: 2
Case Contents: 000010101
.
.
.(showing all the cases)
Line Count: 4085
非常感謝 !然後,如果我得到了相當數量的這種情況下編號,我想要顯示如下:案例編號:0 案件內容:000000100 案件編號:1 案件內容:000000101 行數:38等.. 。 怎麼做 ? – YDev 2014-10-19 22:46:55
@Yolk沒問題!請確保接受我的答案,如果它適合你。你的下一個問題有點複雜,所以試着讓它工作,一旦你得到它,在Stack Overflow上發佈另一個問題,我會做出一個單獨的答案。 – 2014-10-19 22:50:33
我只能發佈每90分鐘一次,我已經接受你的答案=)好吧,我得到的輸出是這樣的: 案件編號:-1 案例內容:000000100 行數:4085 我認爲有一點點案件號碼錯誤。 後來我有215套的情況下(在同一文件和相同的格式與第一組數據),我需要做的輸出,如: 案件編號:0 案例內容:000000100 案件編號:1 病例內容:000000101 病例號碼:2 病例內容:000010101 。 。 。(顯示所有的情況) 行數:4085 – YDev 2014-10-19 23:03:27