Using Java 1.8 and JUnit 1.5, am trying to read a sample csv file for standalone sudoku app through which I try to read the CSV file through a JUnit test.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sudoku</groupId>
<artifactId>sudoku</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Project structure:
sudoku
│
├── sudoku.iml
├── src
│ ├── test
│ │ ├── resources
│ │ │ ├─ valid.csv
│ │ └──── invalid.csv
│ │ └── java
│ │ └── com
│ │ └── sudoku
│ │ └── SudokuTest.java
│ └── main
│ ├── resources
│ │ ├── valid.csv
│ │ └── invalid.csv
│ └── java
│ └── com
│ └── sudoku
│ └── Sudoku.java
├── pom.xml
Sudoku.java:
package com.sudoku;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
public class Sudoku {
public static final int SIZE = 9;
private final int[][] board = new int[SIZE][SIZE];
public boolean isValidRow() {
for (int x = 0; x < SIZE; x++) {
if (isValidBoard(board[x])) {
System.out.println("Invalid row: " + Arrays.toString(board[x]));
return false;
}
}
return true;
}
public static boolean isValidBoard(int[] boardMatrix) {
return Arrays.stream(boardMatrix).sum() == 45;
}
public void loadCsvFile(String csvFile) throws Exception {
URL resource = getClass().getClassLoader().getResource(csvFile);
BufferedReader csvReader = new BufferedReader(new InputStreamReader(resource.openStream()));
int x = 0;
String row = null;
while ((row = csvReader.readLine()) != null) {
String[] rows = row.split(",");
int y = 0;
for (String singleRow : rows) {
if (singleRow != null || !"".equals(singleRow)) {
int rowIntegerValue = 0;
try {
rowIntegerValue = Integer.parseInt(singleRow);
}
catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
if (rowIntegerValue < 1 || rowIntegerValue > 9) {
throw new IllegalArgumentException(rowIntegerValue + " is invalid. Must be in between 1 - 9.");
}
board[x][y] = rowIntegerValue;
y++;
}
}
x++;
}
csvReader.close();
}
}
SudokuTest.java
package com.sudoku;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SudokuTest {
@Test
public void isValidRow() throws Exception {
Sudoku sudoku = new Sudoku();
sudoku.loadCsvFile("valid.csv");
System.out.println("hello " + sudoku.isValidRow());
assertTrue(sudoku.isValidRow());
}
}
valid.csv:
9,2,3,4,5,6,7,8,1
4,5,6,7,8,9,1,2,3
7,8,9,1,2,3,4,5,6
5,3,4,5,6,7,8,9,1
2,6,7,8,9,1,2,3,4
8,9,1,2,3,4,5,6,7
3,4,5,6,7,8,9,1,2
6,7,8,9,1,2,3,4,5
9,1,2,4,3,5,6,7,8
When I run the JUnit file manually in IntelliJ IDEA Ultimate Edition, I get the following exception:
java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.sudoku.Sudoku.loadCsvFile(Sudoku.java:150)
at com.sudoku.SudokuTest.isValidRow(SudokuTest.java:15)
java.lang.IllegalArgumentException: 0 is invalid. Must be in between 1 - 9.
at com.sudoku.Sudoku.loadCsvFile(Sudoku.java:156)
at com.sudoku.SudokuTest.isValidRow(SudokuTest.java:15)
This is weird because when using the debugger, it iterates and does parse values - I don't see a single 0 or "" anywhere in my code?
Why does this break?
Is there a better way to rewrite this Sudoku.loadCsvFile()
method?