【发布时间】: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