将一串数字转换为二维数组。

huangapple go评论95阅读模式
英文:

Converting a string of numbers into a 2d array

问题

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class NearestNeighbor {

   public static void main(String[] args) throws FileNotFoundException {
    
    // 创建空数组以存储数据
    double[][] testingVal = new double[75][4]; 
    double[][] trainingVal = new double[75][4];
    String[] trainingClassLabel = new String[75];
    String[] testingClassLabel = new String[75];
    
    // 用户输入文件名
    Scanner input = new Scanner(System.in);
    System.out.print("输入训练文件名:");
    String training = input.nextLine();
    System.out.print("输入测试文件名:");
    String testing = input.nextLine();
    
    // 创建用于存储训练值的数组
    File trainingFile = new File(training);
    Scanner fileScanTraining = new Scanner(trainingFile);
    for (int i = 0; fileScanTraining.hasNext() && i < 75; i++) {
        String line = fileScanTraining.nextLine();
        String[] values = line.split(",", 5);
        for (int j = 0; j < 4; j++) {
            trainingVal[i][j] = Double.parseDouble(values[j]);
        }
        trainingClassLabel[i] = values[4];
    }
    
    // 创建用于存储测试值的数组
    File testingFile = new File(testing);
    Scanner fileScanTesting = new Scanner(testingFile);
    for (int i = 0; fileScanTesting.hasNext() && i < 75; i++) {
        String line = fileScanTesting.nextLine();
        String[] values = line.split(",", 5);
        for (int j = 0; j < 4; j++) {
            testingVal[i][j] = Double.parseDouble(values[j]);
        }
        testingClassLabel[i] = values[4];
    }
    
    // 最后关闭扫描器
    fileScanTraining.close();
    fileScanTesting.close();
    input.close();
  }
}
英文:

Hello I have a file of information in the format of

5.1,3.5,1.4,0.2,Iris-setosa

Each row contains 4 numbers and one string with a total of 75 rows. I am attempting to save the numbers into a 2D array, and then the string into its own 1D array. But I am having trouble converting from the string array I have into a 2D array of double value

...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class NearestNeighbor {
public static void main(String[] args) throws FileNotFoundException {
//creates empty values for our arrays
double [][] testingVal= new double[75][4]; 
double[][] trainingVal= new double[75][4];
String [] trainingClassLabel = new String [75];
String [] testingClassLabel = new String [75];
//User inputs for file names
Scanner input = new Scanner(System.in);
System.out.print(&quot;Enter the name of the training file: &quot;);
String training=input.nextLine();
System.out.print(&quot;Enter the name of the testing file: &quot;);
String testing=input.nextLine();
//creates array for our training values
File trainingFile = new File(training);
Scanner fileScanTraining = new Scanner(trainingFile);
for (int i = 1; fileScanTraining.hasNext(); i++) {
String line = fileScanTraining.nextLine();
trainingVal=line.split(&quot;,&quot;,4);
System.out.println(line); 
}
//creates array for our testing values
File testingFile = new File(testing);
Scanner fileScanTesting = new Scanner(trainingFile);
//Finally we close our scanners
fileScanTraining.close();
fileScanTesting.close();
input.close();
}
}
...

答案1

得分: 0

Sure, here's the translated code:

简单循环可以像下面这样使用 -

        int i = 0;
        while (fileScanTraining.hasNext()){
            String line = fileScanTraining.nextLine();
            String[] splittedVal = line.split(",");
            int j = 0;
            while (j < 4){
                testingVal[i][j] = Double.parseDouble(splittedVal[j]);
                j++;
            }
            i++;
        }
英文:

Simple looping can be used like below -

    int i=0;
while (fileScanTraining.hasNext()){
String line = fileScanTraining.nextLine();
String[] splittedVal=line.split(&quot;,&quot;);
int j=0;
while(j&lt; 4){
testingVal[i][j]=Double.parseDouble(splittedVal[j]);
j++;
}
i++;
}

答案2

得分: 0

你只需要在读取trainingFile内容的循环中做一些更改。

Scanner fileScanTraining = new Scanner(trainingFile);
for (int i = 1; fileScanTraining.hasNextLine(); i++) {
    String line = fileScanTraining.nextLine();
    String[] numbers = line.split(",", 4);
    for (int j = 0; j < 4; j++) {
        trainingVal[i][j] = Double.parseDouble(numbers[j]);
    }
    System.out.println(line);
}
英文:

You just need to make a couple of changes in the loop where you read the contents of trainingFile.

Scanner fileScanTraining = new Scanner(trainingFile);
for (int i = 1; fileScanTraining.hasNextLine(); i++) { // changed this line
    String line = fileScanTraining.nextLine();
    String[] numbers = line.split(&quot;,&quot;, 4); // added this line
    for (int j = 0; j &lt; 4; j++) { // added this loop
        trainingVal[i][j] = Double.parseDouble(numbers[j]);
    }
    System.out.println(line); 
}

huangapple
  • 本文由 发表于 2020年8月16日 11:17:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63432796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定