У меня есть CSV-файл, который отформатирован следующим образом:Неправильный выход с помощью replaceAll()
1,,3,4
3,4,,0
2,,4,2
Я читаю этот файл в строковую переменную под названием текста. Затем, делая это
String newText = text.replaceAll("","0");
В моем выходе, она не будет заменить никакой ценности элементов с 0, который является то, что я хочу, но это также присоединяя 0 до и после того, как элементы, которые действительно имеют значение.
010
0
030
040
030
040
0
000
020
0
040
020
Любые помощь приветствуется!
EDIT: Вот остальная часть моего кода для clairification.
import java.lang.*;
public class DataMatrix {
private double[][] dMatrix;
public DataMatrix(String text) {
String[] line = text.split("\n");
// Creates an array for the CSV file
String[][] sMatrix = new String[line.length][];
for(int i=0; i < line.length; i++)
sMatrix[i] = line[i].split(",");
/*for(int j=0; j < sMatrix.length; j++) {
for(int x=0; x <= line.length; x++) {
if(sMatrix[j][x] !=*/
System.out.println("Original String: ");
for(int x=0; x < sMatrix.length; x++) {
for(int j=0; j <= line.length; j++) {
System.out.println(sMatrix[x][j]);
}
}
dMatrix = new double[sMatrix.length][];
System.out.println("Converted to double: ");
for(int x=0; x < sMatrix.length; x++) {
dMatrix[x] = new double[sMatrix[x].length];
for(int j=0; j <= sMatrix.length; j++) {
dMatrix[x][j] = Double.parseDouble(sMatrix[x][j]);
System.out.println(dMatrix[x][j]);
}
}
}
public void avgRowValue() {
double tempTotal = 0;
for(int x=0; x < dMatrix.length; x++) {
System.out.println("***********************************");
System.out.println("The average for row "+(x+1)+" is: ");
for(int i=0; i < dMatrix[x].length;i++) {
tempTotal += dMatrix[x][i];
double rowAvg = tempTotal/dMatrix[x].length;
System.out.println(rowAvg);
tempTotal = 0;
}
}
}
public void avgColValue() {
double tempTotal = 0;
for(int x=0; x < dMatrix[0].length; x++) {
System.out.println("**************************************");
System.out.println("The average for column "+(x+1)+" is: ");
for(int i=0; i <= dMatrix.length-1; i++)
tempTotal += dMatrix[i][x];
double colAvg = tempTotal/dMatrix.length;
System.out.println(colAvg);
tempTotal = 0;
}
}
public void overallAvgValue() {
double tempTotal = 0;
int count = 0;
for(int x=0; x < dMatrix.length; x++) {
for(int i=0; i <= dMatrix.length; i++) {
tempTotal += dMatrix[x][i];
count++;
}
}
double overallAvg = tempTotal/count;
System.out.println("The overall average for the data matrix is: "+overallAvg);
}
public void maxValue() {
double maxValue = 0;
for(int x=0; x < dMatrix.length; x++) {
for(int i=0; i <= dMatrix.length; i++) {
if(dMatrix[x][i] >= maxValue)
maxValue = dMatrix[x][i];
}
}
System.out.println("The max value for the data matrix is: "+maxValue);
}
public void minValue() {
double minValue = dMatrix[0][0];
for(int x=0; x < dMatrix.length; x++) {
for(int i=0; i <= dMatrix.length; i++) {
if(dMatrix[x][i] <= minValue)
minValue = dMatrix[x][i];
}
}
System.out.println("The min value for the data matrix is: "+minValue);
}
}
и моя главная:
import java.util.*;
public class Assignment1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//Make sure the user passed the command line argument - and nothing else
if (args.length != 1){
System.out.println("Assignment1 takes exactly one command-line argument.");
System.out.println("Usage: java Assignment1 some_file.csv");
System.exit(0);
}
String csvFileName = args[0];
//Instantiate my custom file reader
CSVReader fileReader = new CSVReader();
//Read the file into a string
String text = fileReader.readFile(csvFileName);
String newText = text.replaceAll("", "0");
//Create a new instance of DataMatrix that takes in String CSV file and converts to double array
DataMatrix matrix = new DataMatrix(newText);
boolean done = false;
while(done != true) {
System.out.println("**********************");
System.out.println("CSV Reader Menu: ");
System.out.println("1. Display the average values of each individual row.");
System.out.println("2. Display the averages value of each individual column.");
System.out.println("3. Display the average of the entire data matrix.");
System.out.println("4. Display the maximum value of the entire data matrix.");
System.out.println("5. Display the minimum value of the entire data matrix.");
System.out.println("0. Exit.");
int choice = input.nextInt();
switch(choice) {
case 1: matrix.avgRowValue();
break;
case 2: matrix.avgColValue();
break;
case 3: matrix.overallAvgValue();
break;
case 4: matrix.maxValue();
break;
case 5: matrix.minValue();
break;
case 0: done = true;
break;
default: System.out.println("Invalid input.");
break;
}
}//end of menu loop
}//end of main
}//end of class
@azurefrog ошибочный код ... –
@azurefrog багги например, ', выход должен быть : '0 0', но с вашим кодом ничего нет –
Извините за путаницу @azurefrog! То, что я пытаюсь сделать, - это прочитать файл csv и поместить его в массив (который я уже сделал). Затем, когда одно из пятен в файле csv не имеет значения, и мне нужно назначить ему 0. –