我正在創建文件輸入和文本輸出代碼。 我包括input.txt中和文本的內容就像下面在java中將字拆分爲兩個分區
4
100 Watched
2 Key holders
3 Stamps
30 Data structure textbooks
//entered between every number
,我喜歡做的輸出結果是「鑰匙扣」 ,但在我的代碼,輸出爲「關鍵」
我不知道如何打印鑰匙旁邊的「持有人」一詞。 當然,我認爲其原因是因爲當每個「」被看到時我拼接了這個詞。那麼我怎樣才能分割字符串只有兩個分區,由整數和右側?
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
public class Assignment11 {
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new FileReader("input.txt"));
String entryString;
entryString = in.readLine();
int entryNum = Integer.valueOf(entryString);
int min = Integer.MAX_VALUE;
String result = null;
for(int i = 0; i < entryNum; i++){
String temp;
temp = in.readLine();
String entry[] = temp.split(" ");
int price = Integer.parseInt(entry[0]);
if(price < min){
min = price;
result = entry[1];
}
}
System.out.println(result);
in.close();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
}
'temp.split(」」,2)'? –
它確實有效!謝謝 :) –