作者godpro (神)
看板java
標題[問題] 新手讀檔問題
時間Sun Oct 17 03:51:31 2010
程式碼如下
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
推 cooper6334:1.new FileInputStream()裡面是帶字串,"input.txt" 10/17 04:02
→ cooper6334:2. readLine()的回傳值是字串,是不能強制轉型成int的 10/17 04:03
→ cooper6334:你可以考慮使用Integer.parseInt() 10/17 04:07
→ drema:Reader和Stream不能混用阿...如果你要用BufferedReader 就 10/17 07:19
→ tkcn:樓上說的是錯的,這樣接並沒有錯 10/17 11:10