作者kingofsdtw (不能閒下來!!)
看板java
標題[問題] InputStreamReader.read()練習
時間Wed May 14 12:03:56 2014
問題:
用while 讀取至換行字元(變數i指向換行字元),
for(j=0;j<i;j++)卻會印出換行字元
char[80] i
char[0] char[1] Char[2] Char[3] Char[4]
L O V E '\n'
code:
https://www.dropbox.com/s/rci9tepjqg8mbt1/inputReader.rar
http://ideone.com/7GyCfL
/**Start**/
InputStreamReader s1 = new InputStreamReader(System.in);
while ((ch[i] = (char) s1.read()) != 10) {i++;}
/*output*/
for (int j = 0; j < i; j++)
System.out.print(" \"" + ch[j] + "\"");
System.out.println("不會自動換行");
error:
https://www.dropbox.com/s/ei4ad7tuzt6d4ox/inputreader.jpg
嘗試:
1. Method:"while" check
http://ideone.com/Ljzt1e
2. for(j=0;
j0m< i-10m;j++) 結果正確,但不符合邏輯...
3. google InputStreamBuffer.read();用法
大大們感謝了!本魯菜比巴... 練習都遇到一堆Bug
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.161.138.215
※ 文章網址: http://www.ptt.cc/bbs/java/M.1400040242.A.431.html
※ 編輯: kingofsdtw (1.161.138.215), 05/14/2014 12:05:34
→ stimim:網路上有很多貼程式碼的地方 codepad, gist, ideone 05/14 12:07
ty!!
※ 編輯: kingofsdtw (1.161.138.215), 05/14/2014 12:29:34
推 stimim:然後,對你的問題,我建議你把每個字的ASCII碼印出來看看 05/14 12:14
→ qrtt1:同感。要直接能在版上呈現足夠資料才能增加回復的意願。 05/14 12:19
→ qrtt1:儘可能把問題濃縮到不需要放外部 link。在上一篇有些 link 05/14 12:20
→ qrtt1:已經消失了。這樣發問的型式,我個人就不太有看的意願了 05/14 12:21
→ qrtt1:因為問題可能過沒多久就斷頭了,無法在版上累積成知識。 05/14 12:21
※ 編輯: kingofsdtw (1.161.138.215), 05/14/2014 12:34:50
→ stimim:不知道你有沒有注意到,在ideone上的輸出就是你預期的 05/14 12:34
實際在cmd下跑換行鍵會印出來...,ideone似乎不吃換行鍵 ..
※ 編輯: kingofsdtw (1.161.138.215), 05/14/2014 12:36:51
結果:
https://www.dropbox.com/s/27rchmaljr6go6f/inputreader2.jpg
xd~
※ 編輯: kingofsdtw (1.161.138.215), 05/14/2014 13:00:44
→ stimim:很明顯的,多出來的鍵不是\n,而是某個ASCII碼為13的東西 05/14 13:04
感謝大大 @@!
※ 編輯: kingofsdtw (1.161.138.215), 05/14/2014 13:06:32