0

我想将文本的双精度值放在二维数组上。我的文本有这样的 double 和 string 值

我的文字

deneme deneme 4.5 5.5 6.6 7.5 9.9 9.9

dsdsa sddsf 9.9 8.7 7.5 8.8 4.4 6.5

从字符串中提取双精度值。我的程序输出是

4.5 5.5 6.6 7.5 9.9 9.9 9.9 8.7 7.5 8.8 4.4 6.5

我怎么能做我的输出六重奏。如果我能做到这一点,我将对每一行进行排序

一线:4.5 5.5 6.6 7.5 9.9 9.9

二线:9.9 8.7 7.5 8.8 4.4 6.5

public static void main(String[] args) throws IOException {

    ArrayList<String> puan;
    puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
    System.out.format("%d kayıt okundu.%n", puan.size());

    for (int j = 0; j < puan.size(); j++) {

        String point = puan.get(j);

        String[] edit = point.split("[\\{\\}\\)]");

        for (String s : edit) {

            String a = s.replaceAll("[\\}\\-\\>\\>\\]\\[\\#\\*\\{]+", "");
            String b = a.replaceAll("[\\.]+", ",");
            Scanner scanner = new Scanner(b);

            while (scanner.hasNext()) {

                if (scanner.hasNextDouble()) {

                    Double doubleValue = scanner.nextDouble();
                    List<Double> numbers = new ArrayList<Double>();
                    numbers.add(doubleValue);
                    // System.out.println(Arrays.deepToString(numbers.toArray()));

                    final int col = 6;
                    final int rows = numbers.size() % col == 0 ? numbers.size() / col : numbers.size() / col + 1;
                    double[][] deneme = new double[rows][col];
                    for (int i = 0; i < rows; i++) {
                        for (int k = 0; k < col; k++) {
                            if (i == rows - 1 && k >= numbers.size() % col) 
                                break;
                            deneme[i][k] = numbers.get(k + col * i);       
                            System.out.print( deneme[i][k]+ " ");
                        }// for k end
                        System.out.println(); // go to next line
                    } // for i end

                }// if scan end

                else {

                    //if it is string comes value here
                    String stringValue = scanner.next();

                }// if scan end

            }// while end

        }// for string s

    }// for end

}// main end
4

2 回答 2

2

在内部循环(循环)之后引入换行符k,使用System.out.println().

 for (int i = 0; i < numbers.size(); i++) {
      for (int k = 0; k < numbers.size(); k++) {
         deneme[i][k] = numbers.get(k + (numbers.size() * i));       
         System.out.print( deneme[i][k]+ " ");
      }// for k end
      System.out.println(); // to introduce a newline
 } // for i end
于 2013-07-17T05:43:08.617 回答
1

当您将元素恢复到deneme时,所有元素都将被放入,deneme[0]因为k是 from 0to numbers.size() - 1。您可以像下面这样修复它:

final int col = 6;
final int rows = numbers.size() % col == 0 ? numbers.size() / col : numbers.size() / col + 1;
double[][] deneme = new double[rows][col];
for (int i = 0; i < rows; i++) {
    for (int k = 0; k < col; k++) {
        if (i == rows - 1 && k >= numbers.size() % col) // UPDATE: last line may not contain `col` sized elements
            break;
        deneme[i][k] = numbers.get(k + col * i);       
        System.out.print( deneme[i][k]+ " ");
    }// for k end
    System.out.println(); // go to next line
} // for i end
于 2013-07-17T06:16:01.727 回答