2016-10-01 117 views
0

我正在从另一个程序中读取一个文本文件(我真的只是想保存每行的第一个数字,我正在模拟一个内存和CPU的交互,所以这些代码是在另一个等级:!ParseInt始终为零

1 // Load 0 
0 
14 // CopyToX 
4 // LoadIdxX 32 (load from A-Z table) 
32 
21 // JumpIfEqual 12 
12 
9 // Put 2 (output as char) 
2 
25 // IncX 
20 // Jump 3 
3 
1 // Load 0 
0 
16 // CopyToY 
5 // LoadIdxY 59 (load from 1-10 table) 
59 
21 // JumpIfEqual 27 
27 
9 // Put 1 (output as int) 
1 
1 // Load 1 (because no IncY instruction) 
1 
11 // AddY 
16 // CopyToY 
20 // Jump 15 
15 
1 // Print newline 
10 
9 
2 
50 // End 
65 // Data A-Z 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
0 
1 // Data 1-10 
2 
3 
4 
5 
6 
7 
8 
9 
10 
0 

.1000 
30 

我想只是将其保存到存储器阵列,但在调试它,我看到存储的值是零,我不能弄明白我增加了“[0]”,因为编译器会抛出这个错误没有它

The method parseInt(String) in the type Integer is not applicable for the arguments (String[])

所以,任何帮助都非常感谢这里是代码块!

String line; 
int init=0; 
while((line= Fileread.readLine())!=null) 
    { 
System.out.println(line.length()); 
    if(line.length()==0) 
     break; 
    try 
    { 
     memory[init++]=Integer.parseInt(line.trim().split(" ")[0]); 
     System.out.println(memory[init]); 
    } 
    catch(NumberFormatException e) 
    { 
     init=Integer.parseInt(line.substring(1)); 
    } 
} 
+0

显示文本文件的实际格式。或者你的文本文件每行包含一个数字,因为准确的解决方案取决于文件格式 –

+0

好吧,我包含了整个文本文件@PavneetSingh – mrprogrammer231

+0

,因为你可以看到你也有斜线和字符串,你只需要我的号码 –

回答

1

在您实际打印值之前,您正在递增init。 因此,您的代码正在打印0,因为您尚未将任何内容分配给memory[init]

String line; 
int init=0; 
while((line= Fileread.readLine())!=null) 
    { 
System.out.println(line.length()); 
    if(line.length()==0) 
     break; 
    try 
    { 
     memory[init]=Integer.parseInt(line.trim().split(" ")[0]); 
     System.out.println(memory[init]); 
     init++; 
    } 
    catch(NumberFormatException e) 
    { 
     init=Integer.parseInt(line.substring(1)); 
    } 
} 
0

你的代码可能是这样的:

String line; 
int init=0; 
while((line= Fileread.readLine())!=null) 
    { 
System.out.println(line.length()); 
    if(line.length()==0) 
     break; 
    String num= str.replaceAll("\\D+",""); 

    try 
    { 
     if(num.length() == 0){ 
     memory[init++]=Integer.parseInt(num); 
     System.out.println(memory[init]); 
     } 
    } 
    catch(NumberFormatException e) 
    { 
     init=Integer.parseInt(line.substring(1)); 
    } 
}