BufferedReader类型为什么转化成INT类型,返回时返回的不是INT型而是乱码?

来源:百度知道 编辑:UC知道 时间:2024/07/01 15:58:17
这是我的代码,希望能从指定文件读入数据后转换成INT型并返回
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class EightPuzzle{
private int thePuzzle[] = new int[9];

/***

* Constructor which reads in tile values from standard input

*/
public static void main (String[] args)
{EightPuzzle start= new EightPuzzle();
int []i = start.Puzzle();
System.out.print(i);}

public int[] Puzzle()

{
BufferedReader in = new BufferedReader(

new InputStreamReader( System.in ) );

for (int j=0; j<9; j++)

{
try

{

thePuzzle[j] = Integer.parseInt(in.readLine());

}

catch(Exception e)

{
}

if ( thePuzzle[j] > 8 || thePuzzle[j] < 0)

{

System.err.print("The tile number must be an");

Sys

不好意思 我从你代码没有看到 从文本里面读入数据

只看到你是从 System.in 从键盘读

...厄 我加点注释吧..

BufferedReader in = new BufferedReader(

new FileReader( "1.txt" ) );

//把你的文本信息读出来..假设只有一行
String tempStr = in.readLine();

//如果没读倒nums赋值null,读倒后按空格分离这些数字
//例如0 2 3 4 5
//nums里面就放了nums[0]=0 以此类推
String [] nums = tempStr != null? tempStr.split("\\s"):null;

if(nums !=null){
for(String num : nums){
System.out.print(Integer.parseInt(num));
}
}

换成while循环

String str = null;
while((str=is.readLine())!=null){

}