【问题标题】:How to save a deleted character with stringbuilder如何使用 stringbuilder 保存已删除的字符
【发布时间】:2018-03-25 14:15:21
【问题描述】:

是否可以保存使用 Stringbuffer 删除的字符或删除除“i”索引上的字符之外的所有字符 这是我从 txt 文件中输入的内容:

3;8;4;5;3;2
3 4 5 1 2 3
9;8;3;2;3;4
9 8 9 7 8 1

我需要总结每一行,看看哪一行是大多数偶数,所以我决定读一整行,用空格分隔字符,然后在字符串生成器的帮助下删除除“i”上的一个之外的所有字符位置,最后保存在二维数组中。 也许你会有更好的想法怎么做?

我的代码:

package Operacje_na_plikach;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Zad3 {
    /*
    Plik tekstowy ‘dane.txt’ ma postać:
3;8;4;5;3;2
3 4 5 1 2 3
9;8;3;2;3;4
9 8 9 7 8 1
Pobierz z pliku tekstowego kolejne wiersze liczb i wypisz na ekranie numer wiersza, w którym występuje najwięcej elementów parzystych.
     */
    public static String[][] odczyt(String nazwa)
    {
        String[][] arr = new String[1][1];
        int[] suma = new int[1];
        int max = -1;
        int wiersz=-1;
        String text = null;
        try {
            FileReader reader = new FileReader(nazwa);
            Scanner sc = new Scanner(reader);
            while(sc.hasNextLine())
            {
                arr=Arrays.copyOf(arr,arr.length+1);
                text = sc.nextLine().replaceAll(";"," ");
                int[] temp= new int[text.length()];
                StringBuilder sb = new StringBuilder(text);
                for (int i = 0; i <temp.length ; i++) {
                 temp[i]=sb.delete();
                }
                for (int i = 0; i < ; i++) {
                    for (int j = 0; j < ; j++) {
                        arr[i][j] = text
                    }
                }
            }

            /* while (sc.hasNextLine())
            {
                arr=Arrays.copyOf(arr,arr.length+1);
                text = sc.nextLine().replaceAll("[;]"," ");

                for (int i = 0; i <arr.length ; i++) {
                    while(text!=null)
                    {
                        int temp = Integer.parseInt(text);
                    }
                    for (int j = 0; j <arr.length ; j++) {

                        arr[i][j] = text.nextInt();
                        if(arr[i][j]%2==0)
                        {
                            suma[i]+=arr[i][j];
                            if(suma[i]>max)
                            {
                                max = suma[i];
                                wiersz=i;
                            }
                        }
                    }
                }
            }
            System.out.println("Najwiecej liczb parzystych jest w wierszu: " + wiersz);
*/



            sc.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return arr;
    }

    public static void main(String[] args) {
        int[][] arr = odczyt("dane.txt");
    }
}

【问题讨论】:

  • 为了澄清 - 您希望获得此文件的输出是什么?
  • 偶数和最大的行数

标签: java stringbuffer except


【解决方案1】:

我认为您可能过于复杂了。您可以流式传输文件的行,解析它们,并将它们映射到偶数的总和。然后,您可以遍历总和数组并找到最大元素的索引(也可以使用流来完成,只是为了好玩):

long[] sums =
    Files.lines(Paths.get("dane.txt"))
         .mapToLong(s -> Arrays.stream(s.split("[ ;]"))
                               .mapToInt(Integer::parseInt)
                               .filter(i -> i % 2 == 0)
                               .sum())
         .toArray();

int lineNum =
    IntStream.range(0, sums.length)
             .boxed()
             .max(Comparator.comparingLong(i -> sums[i]))
             .get();

【讨论】:

  • 谢谢,但我按照你看到的方式做了,因为你的解决方案超出了我的技能。我正在学习java,我认为我离解决问题还很远,就像你刚刚做的那样。我的水平:面向对象编程简介:D,但非常感谢!
猜你喜欢
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
相关资源
最近更新 更多