gpt4 book ai didi

java - ParseInt 不断得到零

转载 作者:行者123 更新时间:2023-12-01 09:26:18 24 4
gpt4 key购买 nike

我正在从另一个程序读取一个文本文件,就是这个(我真的只是想保存每行的第一个数字。我正在模拟内存和CPU交互,所以这些是另一个类中的代码:

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]”,因为如果没有它,编译器会抛出此错误

Integer 类型中的方法 parseInt(String) 不适用于参数 (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));
}
}

最佳答案

在实际打印该值之前,您将递增 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));
}
}

关于java - ParseInt 不断得到零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39809453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com