Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Using JUnit 5 - Reading Suduko CSV file throws NumberFormatException: For input string: “”

User_Q4NJA
Member Posts: 1 Green Ribbon
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?