Java 从.txt文件向数组中添加数字
嘿,伙计们,我对java相当陌生,当我试图将一个名为compact.txt的文件中的数字添加到数组中时,我遇到了这个问题。以下是我目前的代码:Java 从.txt文件向数组中添加数字,java,arrays,Java,Arrays,嘿,伙计们,我对java相当陌生,当我试图将一个名为compact.txt的文件中的数字添加到数组中时,我遇到了这个问题。以下是我目前的代码: public void compactArray(){ try{ Scanner scan = new Scanner(new File("compact.txt")); while(scan.hasNextInt()){ num++; } int [] a
public void compactArray(){
try{
Scanner scan = new Scanner(new File("compact.txt"));
while(scan.hasNextInt()){
num++;
}
int [] a = new int[num];
Scanner in = new Scanner(new File("compact.txt"));
while(counter < num){
a[counter] = in.nextInt();
counter++;
}
System.out.println(Arrays.toString(a));
}catch(IOException bob){
bob.getMessage();
}
}
public void compactArray(){
试一试{
扫描仪扫描=新扫描仪(新文件(“compact.txt”);
while(scan.hasNextInt()){
num++;
}
int[]a=新的int[num];
扫描仪输入=新扫描仪(新文件(“compact.txt”);
while(计数器
这段代码的问题在于它从未停止运行。首先,我的代码读取compact.txt,然后计算它必须计算的数字量,以计算数组的大小。然后我创建另一个scanner变量,将compact.txt中的数字添加到数组中。我使用一个计数器变量作为在所需数量的数字被添加到数组a中时停止的一种方法。我不太确定问题出在哪里,但它一直在运行,没有到达应该打印阵列的位置。谁能帮帮我吗。非常感谢。你应该打电话给我
scan.nextInt();
在第一个循环中。您从不移动光标,因此您会一直读取第一个元素
但是,解决方案需要对数据集进行两次检查。您可能需要使用ArrayList,这是一个可以调整大小的数组。这样,您就不需要先清点您的文件。您应该调用
scan.nextInt();
在第一个循环中。您从不移动光标,因此您会一直读取第一个元素
但是,解决方案需要对数据集进行两次检查。您可能需要使用ArrayList,这是一个可以调整大小的数组。这样,您就不需要先清点您的文件。更改
while(scan.hasNextInt()){ here is the problem, This loop never move to next integer. You need to call to scan.nextInt() required to move next integer
num++;
}
到
改变
到
您在那里做错了什么:您应该只使用一个
扫描仪
对象
更具体地说,在您的情况下出现的错误如下:您正在检查扫描仪是否在中有下一个int,而(scan.hasNextInt()){
,但您从未实际读取该int。因此它将永远循环
正确的工作代码为:
public void compactArray(){
List<Integer> ints = new ArrayList<>();
try{
Scanner scan = new Scanner(new File("compact.txt"));
while(scan.hasNextInt()){
ints.add(in.nextInt());
}
}catch(IOException ex){
ex.getMessage();
}
System.out.println(Arrays.toString(ints.toArray(new int[ints.size()])));
}
public void compactArray(){
List ints=new ArrayList();
试一试{
扫描仪扫描=新扫描仪(新文件(“compact.txt”);
while(scan.hasNextInt()){
ints.add(in.nextInt());
}
}捕获(IOEX异常){
例如getMessage();
}
System.out.println(Arrays.toString(ints.toArray(新的int[ints.size()]));
}
我还更改了代码的以下几点:
- 现在内部使用一个
来存储整数。因此无需再进行计数列表
- 为异常指定了一个有意义的名称
- 在
中,首先将System.out.println
转换为数组,然后给出列表
-表示字符串
扫描仪
对象
更具体地说,在您的情况下出现的错误如下:您正在检查扫描仪是否在中有下一个int,而(scan.hasNextInt()){
,但您从未实际读取该int。因此它将永远循环
正确的工作代码为:
public void compactArray(){
List<Integer> ints = new ArrayList<>();
try{
Scanner scan = new Scanner(new File("compact.txt"));
while(scan.hasNextInt()){
ints.add(in.nextInt());
}
}catch(IOException ex){
ex.getMessage();
}
System.out.println(Arrays.toString(ints.toArray(new int[ints.size()])));
}
public void compactArray(){
List ints=new ArrayList();
试一试{
扫描仪扫描=新扫描仪(新文件(“compact.txt”);
while(scan.hasNextInt()){
ints.add(in.nextInt());
}
}捕获(IOEX异常){
例如getMessage();
}
System.out.println(Arrays.toString(ints.toArray(新的int[ints.size()]));
}
我还更改了代码的以下几点:
- 现在内部使用一个
来存储整数。因此无需再进行计数列表
- 为异常指定了一个有意义的名称
- 在
中,首先将System.out.println
转换为数组,然后给出列表
-表示字符串
Java
问题就像一场竞赛。请在几秒钟内回答,宝贝!+1用于按原样回答问题并提示不要阅读两次。如果我调用scan.nextInt()在第一个循环中,我如何知道数组中有多少项。例如,您不必先计算数组中的项,然后创建数组,最后将项添加到数组中??您可以保留num++来计算元素。但是您还需要使用scan.nextInt()将光标移到下一个元素这样你就可以退出游戏了loop@BobbyhasNextInt不使用int,因此您在您的版本中一遍又一遍地计算相同的int。这些Java
问题就像一场竞赛。请在几秒钟内回答,宝贝!感谢您的详细解释。我现在明白为什么了,我的代码可以工作:)。感谢您的详细解释。我现在明白为什么了我的代码可以工作:)。