在输入中加倍时,仅在整数变量中存储整数值
因此,基本上,我试图从读取的文件中获取整数值,我想将每个值存储在其自己的arraylist中。
从文件获取读取的文件格式如下
1002 53 1 82 169 120 80 237.6 239.0 177.6 42.5 885.8 7.4
1004 53 1 83.7 169 110 70 160.2 173.4 115.8 44.0 73.5 8.2
1006 48 1 81.1 158 130 70 102.6 173.4 100.4 61.8 73.5 5.2
等...
这是我的代码:
public class Test {
public static void main(String[] args) {
ArrayList<Integer> integerArrayList = new ArrayList();
ArrayList<Double> doubleArrayList = new ArrayList();
String filePath = "records.txt";
try { // Methodology Store in string, split on spaces, covert to int or double,
// add to the variables directly from the Arraylist.
Scanner input = new Scanner(new File(filePath));
Integer integerVal = 0;
Double doubleVal = 0.0;
while (input.hasNextLine() ) {
integerVal = input.nextInt();
if (integerVal instanceof Integer) {
integerArrayList.add(integerVal);
}
doubleVal = input.nextDouble();
if (doubleVal instanceof Double) {
doubleArrayList.add(doubleVal);
}
}
System.out.println(integerArrayList);
System.out.println(doubleArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我试图逐步调试,问题是它将每个值存储在两个变量中,如预期,直到达到双值...
如果您还有其他解决方案/方法,请告诉我,谢谢。
So basically I am trying to get only Integer values from the file being read, I want to store the values each in its own ArrayList.
The data formatting from the file getting read is as following
1002 53 1 82 169 120 80 237.6 239.0 177.6 42.5 885.8 7.4
1004 53 1 83.7 169 110 70 160.2 173.4 115.8 44.0 73.5 8.2
1006 48 1 81.1 158 130 70 102.6 173.4 100.4 61.8 73.5 5.2
etc...
Here is my chuck of code:
public class Test {
public static void main(String[] args) {
ArrayList<Integer> integerArrayList = new ArrayList();
ArrayList<Double> doubleArrayList = new ArrayList();
String filePath = "records.txt";
try { // Methodology Store in string, split on spaces, covert to int or double,
// add to the variables directly from the Arraylist.
Scanner input = new Scanner(new File(filePath));
Integer integerVal = 0;
Double doubleVal = 0.0;
while (input.hasNextLine() ) {
integerVal = input.nextInt();
if (integerVal instanceof Integer) {
integerArrayList.add(integerVal);
}
doubleVal = input.nextDouble();
if (doubleVal instanceof Double) {
doubleArrayList.add(doubleVal);
}
}
System.out.println(integerArrayList);
System.out.println(doubleArrayList);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I tried to debug step by step, the problem is it stores every value in both variables as expected until it reaches the double values...
If you have any other solution/ approach please let me know, Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在食用之前,测试下一个令牌是
int
(或double
)。如果不是跳过令牌。像Test if the next token is an
int
(or adouble
) before you consume it. If it isn't skip the token. Something like,您是说您只想提取237而不是237.6?
您也可以使用此功能检查下一个值
Do you mean you just wanted to extract 237 and not 237.6?
Also you can use this function to check the next value