0
所以我有這種方法應該讀取文件並檢測符號後面的字符是數字還是單詞。如果它是一個數字,我想刪除它前面的符號,將該數字轉換爲二進制數,並將其替換爲文件中的數字。如果它是一個單詞,我想首先將字符設置爲數字16,但是,如果使用另一個單詞,我想將1添加到原始數字。將一種方法的輸出替換爲另一種方法的輸出
下面是我使用的輸入:
這裏是我的方法:
try {
ReadFile files = new ReadFile(file.getPath());
String[] anyLines = files.OpenFile();
int i;
int wordValue = 16;
// to keep track words that are already used
Map<String, Integer> wordValueMap = new HashMap<String, Integer>();
for (String line : anyLines) {
if (!line.startsWith("@")) {
continue;
}
line = line.substring(1);
Integer binaryValue = null;
if (line.matches("\\d+")) {
binaryValue = Integer.parseInt(line);
}
else if (line.matches("\\w+")) {
binaryValue = wordValueMap.get(line);
// if the map doesn't contain the word value, then assign and store it
if (binaryValue == null) {
binaryValue = wordValue;
wordValueMap.put(line, binaryValue);
++wordValue;
}
}
// --> I want to replace with this
System.out.println(Integer.toBinaryString(binaryValue));
}
for (i=0; i<anyLines.length; i++) {
// --> Here are a bunch of instructions that replace certain strings - they are the lines after @ symbols <--
// --> I'm not going to list them ... <--
System.out.println(anyLines[i]);
所以現在的問題是,我該如何取代那些用(「@」開頭的行線逐線),爲了?
我基本上要輸出看起來像這樣:
101
1110110000010000
10000
1110001100001000
10001
1110101010001000
10001
1111000010001000
10000
1110001110001000
10010
1110001100000110
10011
1110101010000111
10010
1110101010000111
謝謝,但如果@符號後面有一個單詞怎麼辦? – rudna1010 2011-06-01 00:32:43
@bamana,你用什麼作爲你的輸入? – rudna1010 2011-06-01 00:45:53
我使用了上面提供的相同輸入(您顯示的第一個代碼塊)。 – bamana 2011-06-01 00:55:31