Respuesta :
Answer:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class CSVReader {
ArrayList fields;
public CSVReader() {
fields = new ArrayList();
}
public void readFile(String filename) throws FileNotFoundException {
File inputFile = new File(filename);
String word;
String line;
int index = 0;
int ind2 = 0;
int valPrice = 0, valSqft = 0, valSum = 0;
Scanner in = new Scanner(inputFile);
while (in.hasNextLine()) {
line = in.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(",");
fields.add(new ArrayList());
while (lineScanner.hasNext()) {
word = lineScanner.next();
if (index != 0) {
if (ind2 == 2) {
System.out.print(", " + word);
} else if (ind2 == 8) {
System.out.printf(" $" + Integer.valueOf(word) / 1000);
if (Integer.valueOf(word) % 1000 > 0) {
System.out.printf(",%.2f", (float) Integer.valueOf(word) % 1000);
} else {
System.out.print(",000.00");
}
valPrice = Integer.valueOf(word);
valSum += Integer.valueOf(word);
} else if (ind2 == 9) {
System.out.print(" SF " + word);
valSqft = Integer.valueOf(word);
} else if (ind2 == 11) {
System.out.print(" beds " + word);
System.out.printf(" baths price/sf: $ %.2f\n", (float) valPrice / valSqft);
} else if (ind2 > 0) {
System.out.print(" " + word);
} else if (ind2 == 0) {
System.out.print(word);
}
}
((ArrayList) fields.get(index)).add(word);
ind2++;
}
lineScanner.close();
index++;
ind2 = 0;
valPrice = 0;
valSqft = 0;
}
System.out.printf(
"There is a total of " + (numberOfRows() - 1) + " homes on the market with an average price of $"+(valSum / (numberOfRows() - 1))/1000+",%.2f",
(float) (valSum / (numberOfRows() - 1))%1000);
in.close();
}
public int numberOfRows() {
return fields.size();
}
public int numberOfFields(int row) {
if (row < 0) {
return 0;
}
if (row >= fields.size()) {
return 0;
}
return ((ArrayList) fields.get(row)).size();
}
String field(int row, int column) {
if (row >= 0 && row < fields.size()) {
if (column >= 0 && column < fields.size()) {
return (String) ((ArrayList) fields.get(row)).get(column);
} else
return "";
} else
return "";
}
public static void main(String[] args) throws FileNotFoundException {
CSVReader test = new CSVReader();
test.readFile("R.csv");
}
}
Explanation: