sectioning 2d array from input file java - java

Can someone help me out? A total of 3 sections, section 1 is the first index, the sub sequential sections are the sum total of every 5 index's there after. Do not know why I keep getting an exception error.
public class Main {
public static void main(String[] args) throws IOException {
double min, max, sum = 0, avg = 0;// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat(".0");// DecimalFormat class is used to format the output
DecimalFormat df1 = new DecimalFormat(".00");
int rows = 0, cols = 0;
String names[] = null;
double scores[][] = null;
double scoressec[][] = null;
BufferedWriter writer = null;
try { //Opening the input file
Scanner sc = new Scanner(new File("Input.txt"));//Reading the no of rows and columns
rows = sc.nextInt();//check
cols = sc.nextInt();
names = new String[rows];
scores = new double[rows][cols - 1];
scoressec = new double[rows][0];
/* Getting the data from the input file
* and populate those values into array
*/
for (int i = 0; i < rows; i++) {
names[i] = sc.next();
for (int j = 0; j < cols - 1; j++) {
scores[i][j] = sc.nextDouble();
if (j == 0) {
scoressec[i][1] = scores[i][1];
} else if (j == 5) {
scoressec[i][2] += scores[i][j] - scores[i][1];
} else {
scoressec[i][3] += scores[i][j] - scoressec[i][2];
}
}
} //closing the input file`

Related

Accuracy of Multi Layer Neural Network using Backpropagation around 86% normal?

Recently, I started trying to train a neural network using backpropagation. The network structure is 784-512-10, and I used the Sigmoid activation function. When I tested a single-layer network on the MNIST dataset, I got around 90%. My results are around 86% with this multi-layer network, is this normal? Did I get the backpropagation part wrong?
Here is my code:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class NeuralNetwork{
public static double learningRate = 0.01;
public static int epoch = 15;
public static int ROWS = 28;
public static int COLUMNS = 28;
public static int INPUT = ROWS * COLUMNS;
public static int outNum = 10;
public static int hiddenNum = 512;
public static double[][] weights2 = new double[outNum][hiddenNum];
public static double[] bias2 = new double[outNum];
public static double[][] weights1 = new double[hiddenNum][INPUT];
public static double[] bias1 = new double[outNum];
private static final double TRAININGSIZE = 10;
public static double[][] inputs = new double[outNum][INPUT];
private static final double[][] target = new double[outNum][outNum];
private static final ArrayList<String> filenames = new ArrayList<>();
private static final ArrayList<Integer> yetDone = new ArrayList<>();
public static double[] actual = new double[outNum];
public static Random rand = new SecureRandom();
public static Scanner input = new Scanner(System.in);
public static void main(String[]args) throws Exception {
System.out.println("1. Learn the network");
System.out.println("2. Guess a number");
System.out.println("3. Guess file");
System.out.println("4. Guess All Numbers");
System.out.println("5. Guess image");
switch (input.nextInt()){
case 1:
learn();
break;
case 2:
guess();
break;
case 3:
guessFile();
break;
case 4:
guessAll();
break;
}
}
public static void guessAll() throws IOException, ClassNotFoundException {
System.out.println("Recognizing...");
/*
for(int x = 1; x < 60000; x++){
filenames.add("data/" + String.format("%05d",x) + ".txt");
}
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("network.ser")));
Layers lay = (Layers) ois.readObject();
int correct = 0;
for (String z : filenames) {
double[] a = scan(z,0);
correct += getBestGuess(sigmoid(lay.step(a))) == actual[0] ? 1 : 0;
}
System.out.println("Training: " + correct + " / " + filenames.size() + " correct.");
filenames.clear();
*/
for(int x = 60000; x < 70000; x++){
filenames.add("data/" + String.format("%05d",x) + ".txt");
}
ObjectInputStream oiss = new ObjectInputStream(new BufferedInputStream(new FileInputStream("network1.ser")));
Layers lays1 = (Layers) oiss.readObject();
ObjectInputStream oiss2 = new ObjectInputStream(new BufferedInputStream(new FileInputStream("network2.ser")));
Layers lays2 = (Layers) oiss2.readObject();
int corrects = 0;
for (String z : filenames) {
double[] a = scan(z,0);
corrects += getBestGuess(sigmoid(lays2.step(sigmoid(lays1.step(a))))) == actual[0] ? 1 : 0;
}
System.out.println("Testing: " + corrects + " / " + filenames.size() + " correct.");
System.out.println("Done!");
}
public static void makeList(){
for(int index = 0; index < TRAININGSIZE; index++){
int indices = rand.nextInt(yetDone.size() - 1) + 1;
filenames.add("data/" + String.format("%05d",yetDone.get(indices)) + ".txt");
yetDone.remove(indices);
}
prepareData();
for(int indices = 0; indices < outNum; indices++) {
for(int index = 0; index < outNum; index++){
target[indices][index] = 0;
}
target[indices][(int)actual[indices]] = 1;
}
}
public static void prepareData(){
for(int index = 0; index < outNum; index++){
try {
inputs[index] = scan(filenames.get(index), index);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
public static double[] scan(String filename, int index) throws FileNotFoundException {
Scanner in = new Scanner(new File(filename));
double[] a = new double[INPUT];
for(int i = 0; i < INPUT; i++){
a[i] = in.nextDouble() / 255;
}
actual[index] = in.nextDouble();
return a;
}
public static void guessFile() throws IOException, ClassNotFoundException {
System.out.print("Enter Filename: ");
double[] a = scan(input.next(), 0);
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("network.ser")));
Layers lay = (Layers) ois.readObject();
double[] results = lay.step(a);
System.out.println("This is a " + getBestGuess(sigmoid(results)) + "!");
System.out.println(Arrays.toString(results));
}
public static double guess(double[] a) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("network.ser")));
Layers lay = (Layers) ois.readObject();
double[] results = lay.step(a);
return getBestGuess(sigmoid(results));
}
public static void guess() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("network.ser")));
System.out.println("Input number: ");
Layers lay = (Layers) ois.readObject();
double[] a = new double[INPUT];
for(int index = 0; index < a.length; index++){
a[index] = input.nextInt();
}
double[] results = lay.step(a);
System.out.println("This is a " + getBestGuess(sigmoid(results)) + "!");
System.out.println(Arrays.toString(sigmoid(results)));
}
public static void learn() {
System.out.println("Learning...");
initialise(weights2, outNum, hiddenNum);
initialise(bias2);
initialise(weights1,hiddenNum, INPUT);
initialise(bias1);
Layers lay2 = new Layers(weights2, bias2, outNum, hiddenNum);
Layers lay1 = new Layers(weights1, bias1, hiddenNum, INPUT);
double[] result2 = new double[lay2.outNum];
double[] result1 = new double[lay1.outNum];
double[] a2;
double[] a1;
double cost = 0;
double sumFinal;
for(int x = 0; x < epoch; x++) {
yetDone.clear();
for(int y = 0; y < 60000; y++){
yetDone.add(y);
}
for (int ind = 0; ind < 200; ind++) {
filenames.clear();
makeList();
for (int n = 0; n < lay2.outNum; n++) {
a1 = inputs[n]; //number
result1 = sigmoid(lay1.step(a1));
a2 = result1;
result2 = sigmoid(lay2.step(a2));
for (int i = 0; i < lay2.outNum; i++) {
for (int j = 0; j < lay2.INPUT; j++) {
weights2[i][j] += learningRate * a2[j] * (target[n][i] - result2[i]);
cost += Math.pow((target[n][i] - result2[i]), 2);
}
}
for(int i = 0; i < lay1.outNum; i++){
for(int j = 0; j < lay1.INPUT; j++){
sumFinal = 0;
for(int k = 0; k < lay2.outNum; k++){
// weight * derivSigma(outputHiddenLayer) * 2(out - expected)
sumFinal += result1[k] * (1 - result1[k]) * 2 * (result2[k] - target[n][k]); // * weights2[k][i]
}
weights1[i][j] -= learningRate * a1[j] * sumFinal * result1[i] * (1 - result1[i]);
}
}
}
lay1.update(weights1, bias1);
lay2.update(weights2, bias2);
}
System.out.println("Epoch " + x + ": " + cost);
cost = 0;
}
System.out.println(Arrays.toString(result1));
System.out.println(Arrays.toString(result2));
for(double[] arr : inputs) {
System.out.println("This is a " + getBestGuess(sigmoid(lay2.step(sigmoid(lay1.step(arr))))) + "!");
}
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("network1.ser")))) {
oos.writeObject(lay1);
} catch (IOException ex) {
ex.printStackTrace();
}
try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("network2.ser")))) {
oos.writeObject(lay2);
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Done! Saved to file.");
}
public static double sigmoid(double x){
return 1 / (1 + Math.exp(-x));
}
public static double[] sigmoid(double[] weights){
for(int index = 0; index < weights.length; index++){
weights[index] = sigmoid(weights[index]);
}
return weights;
}
public static void initialise(double[] bias){
Random random = new Random();
for(int index = 0; index < bias.length; index++){
bias[index] = random.nextGaussian();
}
}
public static void initialise(double[][] weights, int outNum, int INPUT){
Random random = new Random();
for(int index = 0; index < outNum; index++){
for(int indice = 0; indice < INPUT; indice++){
weights[index][indice] = random.nextGaussian();
}
}
}
public static int getBestGuess(double[] result){
double k = Integer.MIN_VALUE;
double index = 0;
int current = 0;
for(double a : result){
if(k < a){
k = a;
index = current;
}
current++;
}
return (int)index;
}
}
class Layers implements Serializable {
private static final long serialVersionUID = 8L;
double[][] weights;
double[] bias;
int outNum;
int INPUT;
public Layers(double[][] weights, double[] bias, int outNum, int INPUT){
this.weights = weights;
this.bias = bias;
this.outNum = outNum;
this.INPUT = INPUT;
}
public void update(double[][] weights, double[] bias){
this.weights = weights;
this.bias = bias;
}
public double[] step(double[] aa){
double[] out = new double[outNum];
for (int index = 0; index < outNum; index++) {
for (int indices = 0; indices < INPUT; indices++) {
out[index] += weights[index][indices] * aa[indices];
}
}
return out;
}
}
Thanks in advance!

How to input Strings into a 2D array

I have this code and I need to export the strings 'Name','Testav','HWav','Lows','grade' into a 2D array with the columns being the following respectively. I then need to export the 2D array into a csv file. Any help would be greatly appreciated.
import java.util.*;
import java.io.*;
import java.io.PrintWriter;
import java.text.*;
public class ComputeGrades {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.printf("Please enter the name of the input file: ");
String input_name = in.next();
System.out.printf("Please enter the name of the output CSV file: ");
String csv_name = in.next();
System.out.printf("Please enter the name of the output pretty-print file: ");
String pretty_name = in.next();
processGrades(input_name, csv_name, pretty_name);
System.out.printf("\nExiting...\n");
}
public static void processGrades (String input_name, String csv_name, String pretty_name)
{
PrintWriter csv = null;
PrintWriter pretty = null;
String[][] data = readSpreadsheet(input_name);
boolean resultb = sanityCheck(data);
int length = data.length;
ArrayList<String> test_avg = new ArrayList<String>();
ArrayList<String> HW_avg = new ArrayList<String>();
ArrayList<String> NAME = new ArrayList<String>();
ArrayList<String> ColN = new ArrayList<String>();
ArrayList<String> Hell = new ArrayList<String>();
String[][] kill_me = new String[length][];
for(int row = 1; row<length; row++)
{
String name = data[row][0];
String name2 = data[row][1];
String Name = name+" "+name2;
int test1 = Integer.parseInt(data[row][2]);
int test2 = Integer.parseInt(data[row][3]);
int test3 = Integer.parseInt(data[row][4]);
int Test = (test1+test2+test3)/3;
String Testav = Integer.toString(Test);
int hw1 = Integer.parseInt(data[row][5]);
int hw2 = Integer.parseInt(data[row][6]);
int hw3 = Integer.parseInt(data[row][7]);
int hw4 = Integer.parseInt(data[row][8]);
int hw5 = Integer.parseInt(data[row][9]);
int hw6 = Integer.parseInt(data[row][10]);
int hw7 = Integer.parseInt(data[row][11]);
int HW = (hw1+hw2+hw3+hw4+hw5+hw6+hw7)/7;
String HWav = Integer.toString(HW);
int[] trying = {Test, HW};
int low = find_min(trying);
String Lows = Integer.toString(low);
String grade = null;
if(low>=90)
{
grade ="A";
}
if(low < 90 && low>= 80)
{
grade = "B";
}
if(low <80 && low>=70)
{
grade ="C";
}
if(low<70 && low>=60)
{
grade="D";
}
if(low<60)
{
grade = "F";
}
test_avg.add(Testav);
HW_avg.add(HWav);
NAME.add(Name);
Hell.add(Name);
Hell.add(Testav);
Hell.add(HWav);
Hell.add(Lows);
Hell.add(grade);
}
System.out.println(Hell);
System.out.printf("\n");
File csvFile = new File(csv_name);
try (PrintWriter csvWriter = new PrintWriter(new FileWriter(csvFile));){
Hell.stream().forEach(csvWriter::println);
} catch (IOException e) {
}
}
public static int find_min(int[] values)
{
int result = values[0];
for(int i = 0; i<values.length; i++)
{
if(values[i]<result)
{
result = values[i];
}
}
return result;
}
public static boolean sanityCheck(String[][] data)
{
if (data == null)
{
System.out.printf("Sanity check: nul data\n");
return false;
}
if(data.length<3)
{
System.out.printf("Sanity check: %d rows\n",data.length);
return false;
}
int cols= data[0].length;
for(int row = 0; row<data.length; row++)
{
int current_cols = data[row].length;
if(current_cols!=cols)
{
System.out.printf("Sanity Check: %d columns at rows%d\n", current_cols, row);
return false;
}
}
return true;
}
public static String[][] readSpreadsheet(String filename)
{
ArrayList<String> lines = readFile(filename);
if (lines == null)
{
return null;
}
int rows = lines.size();
String[][] result = new String[rows][];
for (int i = 0; i < rows; i++)
{
String line = lines.get(i);
String[] values = line.split(",");
result[i] = values;
}
return result;
}
public static ArrayList<String> readFile(String filename)
{
File temp = new File(filename);
Scanner input_file;
try
{
input_file = new Scanner(temp);
} catch (Exception e)
{
System.out.printf("Failed to open file %s\n",
filename);
return null;
}
ArrayList<String> result = new ArrayList<String>();
while (input_file.hasNextLine())
{
String line = input_file.nextLine();
result.add(line);
}
input_file.close();
return result;
}
}
input file:
First,Last,Exam1,Exam2,Final,H1,H2,H3,H4,H5,H6,H7
Ping,Milledge,43,59,68,69,62,43,60,38,37,40
Elisa,Oltz,76,94,73,100,99,100,90,97,100,92
Leonard,Havers,67,95,57,69,95,71,68,61,93,61
Setsuko,Lovera,78,100,84,89,88,92,65,85,66,97
Franklyn,Degnim,54,74,50,63,78,42,42,41,67,64
Gwyneth,Marsico,61,89,81,59,59,62,88,60,66,66
Abigail,Greep,69,99,93,94,91,85,78,91,69,71
Majorie,Granvold,78,100,100,82,96,100,89,100,100,94
Daphine,Polaco,62,82,88,81,68,89,62,73,90,62
An,Corvera,44,71,37,46,57,42,59,66,54,60
Ayanna,Pensiero,64,42,56,37,53,66,69,52,43,58
Era,Deming,98,81,100,69,65,73,77,78,73,89
Michal,Slentz,73,85,81,82,74,93,81,76,69,81
Corie,Brazen,86,99,66,100,69,97,96,100,70,84
Dona,Tufte,63,54,70,71,55,68,86,66,75,63
Juan,Rohdenburg,78,89,100,91,80,97,92,100,98,100
Orville,Samit,88,63,60,88,81,56,91,76,77,80
Ricky,Knoechel,100,100,93,81,100,90,100,92,100,84
Blythe,Threet,38,68,35,61,63,51,48,72,49,51
Sammie,Wachs,46,53,52,76,50,52,56,68,46,75
Estelle,Veazey,72,87,69,98,96,77,95,91,100,91
Agatha,Keckler,100,92,90,95,85,100,94,85,92,100
Novella,Oros,85,76,100,92,84,77,77,90,86,98
Tanya,Quinlisk,47,78,71,50,79,52,69,66,51,45
Marion,Coltrin,68,68,54,39,61,44,66,58,47,74
Helene,Karow,100,100,75,79,100,100,100,92,89,96
Shonta,Bourek,100,96,90,81,97,84,91,100,100,100
Hyon,Anglemyer,81,76,43,43,47,53,44,60,57,65
Ervin,Kenison,78,53,54,75,55,46,61,75,56,69
Renato,Urch,71,64,64,84,49,57,63,69,81,64
Mikel,Burleigh,88,100,90,100,90,91,90,80,74,74
Val,Royal,100,80,100,99,100,100,76,86,100,96
Jodie,Adolfo,94,77,59,83,67,79,87,82,82,75
Roselee,Lienhard,68,75,58,82,96,62,60,94,68,58
Austin,Holznecht,76,49,79,48,58,68,67,71,70,61
Emelia,Toney,70,95,74,90,99,68,100,66,98,98
Lucy,Rhodd,71,91,100,82,100,93,100,100,71,81
Sacha,Chee,78,71,90,82,74,64,62,87,69,84
Julio,Lackner,56,86,53,88,88,73,57,59,80,85
Salvador,Gretzner,54,83,91,66,78,67,61,84,82,6
export file(csv_name)
name,exam_score,hw_score,min_score,grade
Ping Milledge,56.666667,49.857143,49.857143,F
Elisa Oltz,81.000000,96.857143,81.000000,B
Leonard Havers,73.000000,74.000000,73.000000,C
Setsuko Lovera,87.333333,83.142857,83.142857,B
Franklyn Degnim,59.333333,56.714286,56.714286,F
Gwyneth Marsico,77.000000,65.714286,65.714286,D
Abigail Greep,87.000000,82.714286,82.714286,B
Majorie Granvold,92.666667,94.428571,92.666667,A
Daphine Polaco,77.333333,75.000000,75.000000,C
An Corvera,50.666667,54.857143,50.666667,F
Ayanna Pensiero,54.000000,54.000000,54.000000,F
Era Deming,93.000000,74.857143,74.857143,C
Michal Slentz,79.666667,79.428571,79.428571,C
Corie Brazen,83.666667,88.000000,83.666667,B
Dona Tufte,62.333333,69.142857,62.333333,D
Juan Rohdenburg,89.000000,94.000000,89.000000,B
Orville Samit,70.333333,78.428571,70.333333,C
Ricky Knoechel,97.666667,92.428571,92.428571,A
Blythe Threet,47.000000,56.428571,47.000000,F
Sammie Wachs,50.333333,60.428571,50.333333,F
Estelle Veazey,76.000000,92.571429,76.000000,C
Agatha Keckler,94.000000,93.000000,93.000000,A
Novella Oros,87.000000,86.285714,86.285714,B
Tanya Quinlisk,65.333333,58.857143,58.857143,F
Marion Coltrin,63.333333,55.571429,55.571429,F
Helene Karow,91.666667,93.714286,91.666667,A
Shonta Bourek,95.333333,93.285714,93.285714,A
Hyon Anglemyer,66.666667,52.714286,52.714286,F
Ervin Kenison,61.666667,62.428571,61.666667,D
Renato Urch,66.333333,66.714286,66.333333,D
Mikel Burleigh,92.666667,85.571429,85.571429,B
Val Royal,93.333333,93.857143,93.333333,A
Jodie Adolfo,76.666667,79.285714,76.666667,C
Roselee Lienhard,67.000000,74.285714,67.000000,D
Austin Holznecht,68.000000,63.285714,63.285714,D
Emelia Toney,79.666667,88.428571,79.666667,C
Lucy Rhodd,87.333333,89.571429,87.333333,B
Sacha Chee,79.666667,74.571429,74.571429,C
Julio Lackner,65.000000,75.714286,65.000000,D
Salvador Gretzner,76.000000,71.714286,71.714286,C
export file 2(pretty_name)
name: exam score, hw score, min score, grade
Ping Milledge: 56.67, 49.86, 49.86, F
Elisa Oltz: 81.00, 96.86, 81.00, B
Leonard Havers: 73.00, 74.00, 73.00, C
Setsuko Lovera: 87.33, 83.14, 83.14, B
Franklyn Degnim: 59.33, 56.71, 56.71, F
Gwyneth Marsico: 77.00, 65.71, 65.71, D
Abigail Greep: 87.00, 82.71, 82.71, B
Majorie Granvold: 92.67, 94.43, 92.67, A
Daphine Polaco: 77.33, 75.00, 75.00, C
An Corvera: 50.67, 54.86, 50.67, F
Ayanna Pensiero: 54.00, 54.00, 54.00, F
Era Deming: 93.00, 74.86, 74.86, C
Michal Slentz: 79.67, 79.43, 79.43, C
Corie Brazen: 83.67, 88.00, 83.67, B
Dona Tufte: 62.33, 69.14, 62.33, D
Juan Rohdenburg: 89.00, 94.00, 89.00, B
Orville Samit: 70.33, 78.43, 70.33, C
Ricky Knoechel: 97.67, 92.43, 92.43, A
Blythe Threet: 47.00, 56.43, 47.00, F
Sammie Wachs: 50.33, 60.43, 50.33, F
Estelle Veazey: 76.00, 92.57, 76.00, C
Agatha Keckler: 94.00, 93.00, 93.00, A
Novella Oros: 87.00, 86.29, 86.29, B
Tanya Quinlisk: 65.33, 58.86, 58.86, F
Marion Coltrin: 63.33, 55.57, 55.57, F
Helene Karow: 91.67, 93.71, 91.67, A
Shonta Bourek: 95.33, 93.29, 93.29, A
Hyon Anglemyer: 66.67, 52.71, 52.71, F
Ervin Kenison: 61.67, 62.43, 61.67, D
Renato Urch: 66.33, 66.71, 66.33, D
Mikel Burleigh: 92.67, 85.57, 85.57, B
Val Royal: 93.33, 93.86, 93.33, A
Jodie Adolfo: 76.67, 79.29, 76.67, C
Roselee Lienhard: 67.00, 74.29, 67.00, D
Austin Holznecht: 68.00, 63.29, 63.29, D
Emelia Toney: 79.67, 88.43, 79.67, C
Lucy Rhodd: 87.33, 89.57, 87.33, B
Sacha Chee: 79.67, 74.57, 74.57, C
Julio Lackner: 65.00, 75.71, 65.00, D
Salvador Gretzner: 76.00, 71.71, 71.71, C
This should do it, if you have question about the code, just ask.
public static void processGrades (String input_name, String csv_name, String pretty_name) {
DecimalFormat decimalFormat = new DecimalFormat(".000000");
String[][] data = readSpreadsheet(input_name);
String[][] result = new String[data.length][];
result[0] = new String[]{"name", "exam_score", "hw_score", "min_score", "grade"};
// Export to 2D String array
for(int row = 1; row < data.length; row++) {
String name = data[row][0] + " " + data[row][1];
double testAverage = average(data[row], 2, 5);
double homeworkAverage = average(data[row], 5, 12);
double min = Math.min(testAverage, homeworkAverage);
char grade = (char) (74 - ((int) min / 10));
grade = grade > 'D' ? 'F' : grade;
result[row] = new String[]{
name,
decimalFormat.format(testAverage),
decimalFormat.format(homeworkAverage),
decimalFormat.format(min),
Character.toString(grade)
};
}
// Export 2D array into a csv String
String csv = "";
for (int y = 0; y < result.length; y++) {
for (int x = 0; x < result[y].length - 1; x++) {
csv += result[y][x] + ",";
}
csv += result[y][result[y].length - 1] + "\n";
}
// Save String in file
File file = new File(csv_name);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
bw.write(csv);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static double average(String[] row, int fromIndex, int toIndex) {
double total = 0;
for (int i = fromIndex; i < toIndex; i++) {
total += Integer.parseInt(row[i]);
}
return total / (toIndex - fromIndex);
}

How to determine how big is the treasure map (rows and columns) from a textfile?

I'd like to know how to figure out the rows and columns of a passed textfile.
Suppose the textfile looks like this:
X...................
....................
....................
....................
....................
....................
....................
....................
....................
..X.................
This textfile has 10 rows and 20 columns and I'm facing troubles with how to get those rows and columns for my constructor (DONT WORRY ABOUT "X" symbols). I just would like to know how to get rows and columns from the textfile/ would like to know how to figure out how big the map is.
I need help with the second constructor in the code:
import java.util.Scanner; // Required to get input
import java.io.File; // Required to get input from files
// A 2D treasure map which stores locations of treasures in an array
// of coordinates
public class TreasureMap{
int rows, cols; // How big is the treasure map
Coord [] treasureLocations; // The locations of treasures
Scanner kbd = new Scanner(System.in);
// Prompt the user for info on the treasure map and then create it
public TreasureMap(){
int numberOfTreasures = 0;
System.out.println("Enter map size (2 ints): ");
rows = kbd.nextInt(); cols = kbd.nextInt();
System.out.println("Enter number of treasures (1 int): ");
numberOfTreasures = kbd.nextInt();
treasureLocations = new Coord[numberOfTreasures];
for (int i = 0; i < numberOfTreasures; i++)
{
System.out.println("Enter treasure " + i + " location (2 ints): ");
rows = kbd.nextInt(); cols = kbd.nextInt();
treasureLocations[i] = new Coord(rows, cols);
}
}
// Read the string representation of a map from a file
public TreasureMap(String fileName) throws Exception{
Scanner data = new Scanner(new File(fileName));
int counter = 0;
while(data.hasNextLine())
{
counter++;
}
}
// true if there is treasure at the given (r,c) coordinates, false
// otherwise
// This method does not require modification
public boolean treasureAt(int r, int c){
for(int i=0; i<treasureLocations.length; i++){
Coord coord = treasureLocations[i];
if(coord.row == r && coord.col == c){
return true;
}
}
return false;
}
// Create a string representation of the treasure map
public String toString(){
String [][] map = new String[this.rows][this.cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
map[i][j] = ".";
}
}
for(int i=0; i<treasureLocations.length; i++){
Coord c = treasureLocations[i];
map[c.row][c.col] = "X";
}
StringBuilder sb = new StringBuilder();
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
sb.append(map[i][j]);
}
sb.append("\n");
}
return sb.toString();
}
}
Here's some code you can use to read lines from a file:
File file = new File(fileName);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
int rows = 0;
int cols = 0;
while ((line = br.readLine()) != null) {
// process the line
rows++;
cols = line.length(); // always the size of the last line in the file
}
}
br.close();
Based on your implementation, this would give you width and height of your input file.
public TreasureMap(String fileName) throws Exception{
Scanner data = new Scanner(new File(fileName));
int width = 0;
int height = 0;
while(data.hasNextLine())
{
String line = data.nextLine();
width = Math.max(width, line.length());
height++;
}
System.out.println(width + " x " + height);
}

Printing ListArrays to a txt file

I have a method that is suppose to take 3 arrays and save them to a txt file. But it doesnt print to the list. Here is my code for the method
public static void saveAndExit(int count, String[] makeArray, String[] moduleArray, double [] msrpArray) throws IOException{
File carList = new File("cars_list.txt");
PrintWriter pw = new PrintWriter(carList);
int index = 0;
String[] finalMSRPArray = new String[12];
for (int i = 0; i < msrpArray.length; i++){
finalMSRPArray[i] = String.valueOf(msrpArray[i]);
}
while(count < makeArray.length && count < moduleArray.length && count < msrpArray.length){
pw.write(makeArray[index]+"\n");
pw.write(moduleArray[index]+"\n");
pw.write(finalMSRPArray[index]+"\n");
index++;
}
pw.close();
}
Any help is much appreciated!

Array throws NullPointerException in Java

I have the following little problem... I have this code that uses the method OpenFile() of one class ReadData to read a .txt file and also I have another class ArraysTZones used to create an object that stores 3 arrays (data1,data2,data3) and 3 integers (total1,total2,total3) returned by the method OpenFile(). The problem is that when I try to display each array (data1,data2,data3) using the method getArray() of ArrayTZones it stops and displays the error NullPointerException. Anyone knows how could I fix this?
public static void main (String args[]) throws IOException {
String fileName = ".//data.txt";
int[] def = new int[180];
try {
ReadData file = new ReadData(fileName);
ArraysTZones summaryatz = new ArraysTZones();
summaryatz = file.OpenFile();
for (int i = 0; i < 180; i++)
System.out.print (summaryatz.getArray1()[i] + " ");
System.out.println ("");
System.out.println (summaryatz.getTotal1());
for (int i = 0; i < 180; i++)
System.out.print (summaryatz.getArray2()[i] + " ");
System.out.println ("");
System.out.println (summaryatz.getTotal2());
for (int i = 0; i < 180; i++)
System.out.print (summaryatz.getArray3()[i] + " ");
System.out.println ("");
System.out.println (summaryatz.getTotal3());
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
Heres OpenFile()
public ArraysTZones OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
int numberOfTimeZones = 3;
int[] data1 = new int[180];
int[] data2 = new int[180];
int[] data3 = new int[180];
int total1 = 0;
int total2 = 0;
int total3 = 0;
ArraysTZones atz = new ArraysTZones();
for (int i = 0; i < numberOfTimeZones; i++){
if (i == 0) {
String firstTimeZone = textReader.readLine();
String[] val = firstTimeZone.split ("\\s+");
for (int u = 0; u < val.length; u++)
{
int stats = (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
total1 += stats;
data1[u] = stats;
}
total1= total1/180;
atz.setTotal1(total1);
atz.setArray1(data1);
}
else
if (i == 1) {
String secondTimeZone = textReader.readLine();
String[] val = secondTimeZone.split ("\\s+");
for (int u = 0; u < val.length; u++)
{
int stats = (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
total2 += stats;
data2[u] = stats;
}
total2= total2/180;
atz.setTotal2(total2);
atz.setArray2(data2);
}
else {
String thirdTimeZone = textReader.readLine();
String[] val = thirdTimeZone.split ("\\s+");
for (int u = 0; u < val.length; u++)
{
int stats = (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
total3 += stats;
data3[u] = stats;
}
total3= total3/180;
atz.setTotal3(total3);
atz.setArray3(data3);
}
}
textReader.close();
return atz;
}
The getArray()
public int[] getArray1 () {
return data1;
}
And setArray()
public void setArray1 (int[] farray) {
int[] data1 = new int[180];
//int[] farray = new int[180];
data1 = farray;
}
The problem seems to be here
public void setArray1 (int[] farray)
{
int[] data1 = new int[180];
//int[] farray = new int[180];
data1 = farray;
}
You're declaring a new variable called data1 and storing the content of farray to it.
After that method is done, that variable will be removed, due to his scope.
Remove int[] from the line int[] data1 = new int[180]; (or just remove the whole line .. it is unnecessary) and your data will be stored in the correct variable that was declared for the class.
public void setArray1 (int[] farray) {
data1 = farray;
}
You have to initialize ArraysTZones

Categories