Java 扫描程序未将值分配给数组
每当我使用system.out.print输出一个阵列插槽时,无论调用哪个阵列位置,它都只给我0。我哪里做错了Java 扫描程序未将值分配给数组,java,arrays,file-io,io,Java,Arrays,File Io,Io,每当我使用system.out.print输出一个阵列插槽时,无论调用哪个阵列位置,它都只给我0。我哪里做错了 编辑:我必须关闭并重新启动文件阅读器和扫描仪。谢谢你的帮助 您需要在清点行数后重新启动扫描仪(因为它位于文件的末尾) 扫描仪输入=新扫描仪(myReader); int-arraySize=0; while(input.hasNextLine()){ arraySize++; input.nextLine(); } input.close();// 您尝试在不返回开头的情况下读取文件两
编辑:我必须关闭并重新启动文件阅读器和扫描仪。谢谢你的帮助 您需要在清点行数后重新启动
扫描仪
(因为它位于文件
的末尾)
扫描仪输入=新扫描仪(myReader);
int-arraySize=0;
while(input.hasNextLine()){
arraySize++;
input.nextLine();
}
input.close();// 您尝试在不返回开头的情况下读取文件两次。第一次是数行,第二次是读取数据。由于不返回文件的开头,因此不会在任何位置加载/存储数据。因此,您应该重新启动扫描仪
,例如关闭并重新打开
或者您可能想考虑使用<代码> ARARYLIST/<代码>,因此您只需要读取文件一次。
Scanner input = new Scanner(myReader);
int arraySize = 0;
while(input.hasNextLine()){
arraySize++;
input.nextLine();
}
input.close(); // <-- close it.
myReader = new FileReader(myReaderRef); // Create a new FileReader
input = new Scanner(myReader); // <-- create another scanner instance
List number=new ArrayList();
扫描仪输入=新扫描仪(myReader);
while(input.hasNextLine()){
add(input.nextInt());
input.nextLine();//当nextInt()只读取int标记时,您可能需要使用它来进入下一行。
}
input.close()
在第一个while循环中,光标已转到文件的最后一行。因此,在下一个循环中,它无法找到之后的任何内容。因此,您必须创建2个扫描仪
对象
List<Integer> numbers = new ArrayList<Integer>();
Scanner input = new Scanner(myReader);
while (input.hasNextLine()) {
numbers.add(input.nextInt());
input.nextLine(); // You might need this to get to the next line as nextInt() just reads the int token.
}
input.close()
Scanner input=新扫描仪(新文件(“D:\\numbers.txt”);
int-arraySize=0;
while(input.hasNextLine()){
arraySize++;
input.nextLine();
}
int[]数字;
数字=新整数[arraySize];
input.close();
扫描仪输入2=新扫描仪(新文件(“D:\\numbers.txt”);
while(input2.hasNextLine()){
for(int i=0;i
我尽量简化这个问题
为您的案例向数组添加/删除元素的最简单方法是使用ArrayList对象。通读注释并运行项目
下面的第一个整数列表是文件的原始输入
第二个列表包含数组的打印语句。这些答案可能不是您希望索引的地方,但我相信这会让您走上正确的道路:)
[10,2,5,1,7,4,9,3,6,8]
3.
1.
7.
5.
2.
8.
包cs1410;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Scanner;
导入javax.swing.JFileChooser;
公共类ArrayReader{
公共静态void main(字符串[]args)引发FileNotFoundException{
//创建数组列表
ArrayList answer=新的ArrayList();
//读入信息
JFileChooser chooser=新的JFileChooser();
if(JFileChooser.APPROVE_OPTION!=chooser.showOpenDialog(null)){
返回;
}
File File=chooser.getSelectedFile();
//扫描所选文档
扫描仪s=新扫描仪(文件);
整数计数=0;
//扫描每行并将值放入数组列表中
而(s.hasNextLine()){
int input=s.nextInt();
回答:添加(输入);
}
//卡布姆
System.out.println(应答);
System.out.println(answer.indexOf(1));
System.out.println(answer.indexOf(2));
System.out.println(answer.indexOf(3));
System.out.println(answer.indexOf(4));
System.out.println(答案索引of(5));
System.out.println(answer.indexOf(6));
}
}
刚刚这么做了,我仍然得到了所有插槽的0。如果你这样做,input.close()
文件阅读器不会也关闭吗?这意味着你必须再次初始化它…我还不太熟悉数组列表,所以我只是在尝试我目前所知道的。我可能会考虑在我得到我目前正在使用的东西后再进行试验。谢谢你的意见!
Scanner input = new Scanner(myReader);
int arraySize = 0;
while(input.hasNextLine()){
arraySize++;
input.nextLine();
}
input.close(); // <-- close it.
myReader = new FileReader(myReaderRef); // Create a new FileReader
input = new Scanner(myReader); // <-- create another scanner instance
List<Integer> numbers = new ArrayList<Integer>();
Scanner input = new Scanner(myReader);
while (input.hasNextLine()) {
numbers.add(input.nextInt());
input.nextLine(); // You might need this to get to the next line as nextInt() just reads the int token.
}
input.close()
Scanner input = new Scanner(new File("D:\\numbers.txt"));
int arraySize =0;
while(input.hasNextLine()){
arraySize++;
input.nextLine();
}
int[] numbers;
numbers = new int[arraySize];
input.close();
Scanner input2 = new Scanner(new File("D:\\numbers.txt"));
while(input2.hasNextLine()){
for(int i=0; i < numbers.length; i++){
numbers[i] = input2.nextInt();
}
}
input2.close();
System.out.print(numbers[1]);
10
2
5
1
7
4
9
3
6
8
[10, 2, 5, 1, 7, 4, 9, 3, 6, 8]
3
1
7
5
2
8
package cs1410;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class ArrayReader {
public static void main(String[] args) throws FileNotFoundException {
// Creates an array list
ArrayList<Integer> answer = new ArrayList<Integer>();
// Reads in information
JFileChooser chooser = new JFileChooser();
if (JFileChooser.APPROVE_OPTION != chooser.showOpenDialog(null)) {
return;
}
File file = chooser.getSelectedFile();
// Scan chosen document
Scanner s = new Scanner(file);
int count = 0;
// Scans each line and places the value into the array list
while (s.hasNextLine()) {
int input = s.nextInt();
answer.add(input);
}
// Kaboom
System.out.println(answer);
System.out.println(answer.indexOf(1));
System.out.println(answer.indexOf(2));
System.out.println(answer.indexOf(3));
System.out.println(answer.indexOf(4));
System.out.println(answer.indexOf(5));
System.out.println(answer.indexOf(6));
}
}