Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件中读取数据并将其存储在两个数组中_Java_Arrays - Fatal编程技术网

Java 从文件中读取数据并将其存储在两个数组中

Java 从文件中读取数据并将其存储在两个数组中,java,arrays,Java,Arrays,您好,我在尝试读取文件并获取文件的两列并将它们分别放入各自的数组时遇到问题。任何帮助都将不胜感激!谢谢 public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); // TODO Auto-generated method stub System.out.println("Please enter the name of the file to be read")

您好,我在尝试读取文件并获取文件的两列并将它们分别放入各自的数组时遇到问题。任何帮助都将不胜感激!谢谢

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    // TODO Auto-generated method stub

    System.out.println("Please enter the name of the file to be read");
    String fileName = keyboard.nextLine();
    Scanner chirping = null;//user input for file name
    boolean fileValue = false; //this makes the value false until the correct file name is inputed
    while(!fileValue) {
        try {
            FileReader dates = new FileReader(fileName); // connects to the user inputted file
            chirping = new Scanner(dates); //scans the file
            fileValue = true; //turns file value to true which takes us out of the while loop

            }//end try
        catch(IOException e)  {
            System.out.println("File Not Found, Please Try Again: ");
            fileName = keyboard.nextLine();
        }//end catch
}// end while
    double[] dataSet = new double[30];
    double[] chirpFreq = new double[30];
    double[] temp = new double[30];
    //double[] temp = new double[chirping.nextInt()];
        for(int i = 0; chirping.hasNextDouble(); i++) {
                dataSet[i] = chirping.nextDouble();
        }

        for(int j = 0; j <= dataSet.length; j++) {
            if(j%2 == 0) {
                chirpFreq[j] = dataSet[j];
            }
            else {
                temp[j] = dataSet[j];
            }
        }

        for(int i = 0; i <= chirpFreq.length; i++) {
            System.out.print(chirpFreq[i]+ " ");
        }
        chirping.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
//TODO自动生成的方法存根
System.out.println(“请输入要读取的文件名”);
字符串文件名=keyboard.nextLine();
Scanner chirping=null;//用户输入文件名
boolean fileValue=false;//这将使值为false,直到输入正确的文件名为止
而(!fileValue){
试一试{
FileReader dates=new FileReader(fileName);//连接到用户输入的文件
啁啾=新扫描仪(日期);//扫描文件
fileValue=true;//将文件值转换为true,从而使我们脱离while循环
}//结束尝试
捕获(IOE异常){
System.out.println(“未找到文件,请重试:”;
fileName=keyboard.nextLine();
}//端接
}//结束时
double[]数据集=新的double[30];
double[]chirpFreq=新的双精度[30];
双精度[]温度=新双精度[30];
//double[]temp=新的double[chirping.nextInt()];
for(int i=0;chirping.hasNextDouble();i++){
dataSet[i]=chirping.nextDouble();
}

对于(int j=0;j我通常不使用
nextDouble()
读取文件,因此我不知道您的问题到底是什么,但您可以将代码重构为:

double[]firstColumn=新的双精度[30];
double[]secondColumn=新的双精度[30];
字符串行=”;
int i=0;
//继续读,直到没有东西读为止
while((line=chirping.nextLine())!=null){
//这是一个正则表达式,它根据空格将行拆分为一个数组
//如果数据以空格分隔,则只需使用“”,如果是制表符,则使用“\t”
String[]columns=line.split(\\s+);
firstColumn[i]=Double.parseDouble(列[0]);
secondColumn[i++]=Double.parseDouble(columns[1]);
}
chirping.close();

我通常不使用
nextDouble()
读取文件,因此我不知道您的问题到底是什么,但您可以将代码重构为:

double[]firstColumn=新的双精度[30];
double[]secondColumn=新的双精度[30];
字符串行=”;
int i=0;
//继续读,直到没有东西读为止
while((line=chirping.nextLine())!=null){
//这是一个正则表达式,它根据空格将行拆分为一个数组
//如果数据以空格分隔,则只需使用“”,如果是制表符,则使用“\t”
String[]columns=line.split(\\s+);
firstColumn[i]=Double.parseDouble(列[0]);
secondColumn[i++]=Double.parseDouble(columns[1]);
}
chirping.close();

你到底遇到了什么问题?
j@ScaryWombat是的,这就是错误,告诉我如何解决这个问题,或者我做错了什么?数组索引从
0
开始,范围一直到
length-1
,所以
对于(int j=0;j
@ScaryWombat好的,谢谢!我把我的值分配到了正确的数组中,唯一的问题是我没有填充剩余的索引,它们默认为零。你到底有什么问题?
j@ScaryWombat是的,这是一个错误,告诉了我如何解决这个问题,或者我做错了什么?数组索引从
0
开始,范围一直到
length-1
,因此
用于(int j=0;j
@ScaryWombat好的,我成功了,谢谢!我把我的值分配到了正确的数组中,唯一的问题是我没有填充剩余的索引,它们默认为零。比我的想法更好的语法!@Jeffrey我这样做时出错了。“colums=line.split(\\s+)做什么?@cgrubick噢,糟糕,应该是一个字符串数组,我编辑了我的回答比我的想法更好的语法!@Jeffrey我这样做时出错了。“colums=line.split(\\s+);”做什么?@cgrubick噢,糟糕,应该是一个字符串数组,我编辑了我的回答