简体   繁体   中英

How to save a deleted character with stringbuilder

Is it possible to save a character which was deleted with the Stringbuffer or delete all character except one on 'i' index This is my input from a txt file:

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 need to sum up each line and see in which one is most of even numbers, so i decided to read a whole line, separate characters with a space then with help of string builder delete all characters except one on 'i' position and finally save in two dimentional array. Maybe you'll have some better idea how to do it?

My code:

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");
    }
}

I think you may be over-complicating things. You could stream the lines of the file, parse them, and map them to the sum of the even numbers. You can then go over the array of sums and find the index of the largest element (which can also be done with streams, just for the fun of it):

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM