[問題] 新手讀檔問題

看板java作者 (神)時間15年前 (2010/10/17 03:51), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/2 (看更多)
程式碼如下 import java.io.*; public class test { public static void main(String[] args) { int num1,num2; BufferedReader in = new BufferedReader(new InputStreamReader (new FileInputStream(input.txt))); num1 = in.readLine(); num2 = in.readLine(); num1 = num1 + num2; System.out.print(num1); } } 但一直編不過去 出現 --------------------Configuration: <Default>-------------------- C:\test.java:5: cannot find symbol symbol : variable input location: class test BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(input.txt))); ^ C:\test.java:6: incompatible types found : java.lang.String required: int num1 = in.readLine(); ^ C:\test.java:7: incompatible types found : java.lang.String required: int num2 = in.readLine(); ^ 3 errors 將 num1 = (int)in.readLine(); 也不行 囧.. input.txt 的內容為 0 10 20 我的目地是把 input.txt 的內容拿出來做運算 謝謝各位大大! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.26.30.133

10/17 04:02, , 1F
1.new FileInputStream()裡面是帶字串,"input.txt"
10/17 04:02, 1F

10/17 04:03, , 2F
2. readLine()的回傳值是字串,是不能強制轉型成int的
10/17 04:03, 2F

10/17 04:07, , 3F
你可以考慮使用Integer.parseInt()
10/17 04:07, 3F

10/17 07:19, , 4F
Reader和Stream不能混用阿...如果你要用BufferedReader 就
10/17 07:19, 4F

10/17 11:10, , 5F
樓上說的是錯的,這樣接並沒有錯
10/17 11:10, 5F
文章代碼(AID): #1CkW97YG (java)
文章代碼(AID): #1CkW97YG (java)