This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I'm trying to read in a text file which looks similar to this:
0000000000
0000100000
0001001000
0000100000
0000000000
Here is my code:
public static int[][] readBoard(String fileName) throws FileNotFoundException {
File life = new File(fileName);
Scanner s = new Scanner(life);
int row = s.nextInt();
int columns = s.nextInt();
int [][] size = new int [row][columns];
for (int i=0; i <= row; i++) {
String [] state = new String [columns];
String line = s.nextLine();
state = line.split("");
for (int j=0; i <= columns; i++) {
size[i][j] = Integer.parseInt(state[j]);
}
}
return size;
}
It keeps giving me this error. I think it's the Integer.parseInt(state[j]) that is giving me trouble, but I don't know why.
Exception in thread "main" 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 Project5.readBoard(Project5.java:33)
at Project5.main(Project5.java:9)
I've executed your code with the example input, and you have logical issues in the code. With the exmaple input the code doesn't even reach the parseInt() line where the asked NumberFormatException could be thwrown. I assume you have tried your code in a different input. The Exception message is staithforward, you tried to parse an empty string to number. It's a typical NumberFormatException. The parseInt() function can throw Exception, so your code must be prepared for it.
The other problem is a basic logical issue in your algorithm. Your row and column variables will be populated with the first to integer token from the text. Based on the exampe input the first integer token will be the first row 0000000000 which integer value is 0, and the second token is 0000100000 which will parsed as 100000. So you are trying to initialize an array with these dimensions which is imposible.
To calculate the row count, you have to read the file line by line. And to get the column counts you have the check the length of the lines. (It can open a new question, how do you want to handle the not properly formatted input file, because in the file the line length can be various.)
That means you can only be sure with the dimensions of the board if you have already iterated though the file content. To prevent the multiple iteration you should use dinamic collection instead of a standard array, like ArrayList.
That means while you are read the file line by line, you can process the the characters one after another in a line. In this step you should be concidered about the invalid characters and the potential empty characters in the end of the file. And during this iteration the final collection can be built.
This example shows a potention solution:
private static int processCharacter(char c) {
try {
return Integer.parseInt((Character.toString(c)));
} catch (NumberFormatException e) {
return 0;
}
}
public static List<List<Integer>> readBoard(String fileName) throws FileNotFoundException {
List<List<Integer>> board = new ArrayList<>();
File file = new File(fileName);
FileReader fr = new FileReader(file);
try (BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
line = line.trim(); // removes empty character from the line
List<Integer> lineList = new ArrayList<>();
if(line.length() > 0) {
for (int i = 0; i < line.length(); i++) {
lineList.add(Main.processCharacter(line.charAt(i)));
}
board.add(lineList);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return board;
}
Related
I am working on a game, and I want to use this text file of mythological names to procedurally generate galaxy solar-system names.
When I read the text file, I tell the while-loop I'm using to continue if there is something that's not a name on a given line. That seems to throw an exception in some (not all) areas where there are multiple lines without names.
How can I make the program work without throwing exceptions or reading lines without names on them?
My Code:
public class Rewrite {
public static void main(String[] args) {
loadFromFile();
}
private static void loadFromFile() {
String[] names = new String[1000];
try {
FileReader fr = new FileReader("src/res/names/Galaxy_System_Names.txt");
BufferedReader br = new BufferedReader(fr);
String aLine;
int countIndex = 0;
while ((aLine = br.readLine()) != null) {
// skip lines without names
if (aLine.equals(String.valueOf(System.lineSeparator()))) {
aLine = br.readLine();
continue;
} else if (aLine.equals("&")) {
aLine = br.readLine();
continue;
} else if (aLine.startsWith("(")) {
aLine = br.readLine();
continue;
}
System.out.println(aLine);
// capitalize first letter of the line
String firstLetter = String.valueOf(aLine.charAt(0));
aLine = firstLetter + aLine.substring(1);
names[countIndex++] = aLine;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The Exception Thrown:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
at java.base/java.lang.String.charAt(String.java:702)
at utilities.Rewrite.loadHumanNamesFromFile(Rewrite.java:39)
at utilities.Rewrite.main(Rewrite.java:10)
Text-File sample: This throws an error after the name "amor"
áed
áedán
aegle
aella
aeneas
aeolus
aeron
(2)
&
aeson
agamemnon
agaue
aglaea
aglaia
agni
(1)
agrona
ahriman
ahti
ahura
mazda
aias
aigle
ailill
aineias
aino
aiolos
ajax
akantha
alberic
alberich
alcides
alcippe
alcmene
alcyone
alecto
alekto
alexander
alexandra
alexandros
alf
(1)
alfr
alkeides
alkippe
alkmene
alkyone
althea
alvis
alvíss
amalthea
amaterasu
amen
ameretat
amirani
ammon
amon
amon-ra
amor
&
amordad
amulius
amun
From the docs of the BufferedReader::readLine:
Returns: A String containing the contents of the line, not including any line-termination characters
Thus when you get to this part of the file:
amor
&
It will read the blank line and strip the linebreak character, and all that will be left is an empty String. Therefore it will not be caught by your if statement:
if (aLine.equals(String.valueOf(System.lineSeparator())))
You need to add in a check for isEmpty()
After amor is an empty line. You're trying to get the char at index 0 of an empty line. Since it's an empty line, it obviously has no chars, and as such there's no char at index 0
I am reading a file with comma separated values which when split into an array will have 10 values for each line . I expected the file to have line breaks so that
line = bReader.readLine()
will give me each line. But my file doesnt have a line break. Instead after the first set of values there are lots of spaces(465 to be precise) and then the next line begins.
So my above code of readLine() is reading the entire file in one go as there are no lined breaks. Please suggest how best to efficiently tackle this scenario.
One way is to replace String with 465 spaces in your text with new line character "\n" before iterating it for reading.
I second Ninan's answer: replace the 465 spaces with a newline, then run the function you were planning on running earlier.
For aesthetics and readability I would suggest using Regex's Pattern to replace the spaces instead of a long unreadable String.replace(" ").
Your code could like below, but replace 6 with 465:
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
String content = "DOG,CAT MOUSE,CHEESE";
Pattern p = Pattern.compile("[ ]{6}",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
String newString = p.matcher(content).replaceAll("\n");
System.out.println(newString);
}
My suggestion is read file f1.txt and write to anther file f2.txt by removing all empty lines and spaces then read f2.txt something like
FileReader fr = new FileReader("f1.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("f2.txt");
String line;
while((line = br.readLine()) != null)
{
line = line.trim(); // remove leading and trailing whitespace
if (!line.equals("")) // don't write out blank lines
{
fw.write(line, 0, line.length());
}
}
Then try using your code.
You might create your own subclass of a FilterInputStream or a PushbackInputStream and pass that to an InputStreamReader. One overrides int read().
Such a class unfortunately needs a bit of typing. (A nice excercise so to say.)
private static final int NO_CHAR = -2;
private boolean fromCache;
private int cachedSpaces;
private int cachedNonSpaceChar = NO_CHAR;
int read() throws IOException {
if (fromCache) {
if (cachecSpaces > 0) ...
if (cachedNonSpaceChar != NO_CHAR) ...
...
}
int ch = super.read();
if (ch != -1) {
...
}
return ch;
}
The idea is to cache spaces till either a nonspace char, and in read() either take from the cache, return \n instead, call super.read() when not from cache, recursive read when space.
My understanding is that you have a flat CSV file without proper line break, which supposed to have 10 values on each line.
Updated:
1. (Recommended) You can use Scanner class with useDelimiter to parse csv effectively, assuming you are trying to store 10 values from a line:
public static void parseCsvWithScanner() throws IOException {
Scanner scanner = new Scanner(new File("test.csv"));
// set your delimiter for scanner, "," for csv
scanner.useDelimiter(",");
// storing 10 values as a "line"
int LINE_LIMIT = 10;
// implement your own data structure to store each value of CSV
int[] tempLineArray = new int[LINE_LIMIT];
int lineBreakCount = 0;
while(scanner.hasNext()) {
// trim start and end spaces if there is any
String temp = scanner.next().trim();
tempLineArray[lineBreakCount++] = Integer.parseInt(temp);
if (lineBreakCount == LINE_LIMIT) {
// replace your own logic for handling the full array
for(int i=0; i<tempLineArray.length; i++) {
System.out.print(tempLineArray[i]);
} // end replace
// resetting array and counter
tempLineArray = new int[LINE_LIMIT];
lineBreakCount = 0;
}
}
scanner.close();
}
Or use the BufferedReader.
You might not need the ArrayList to store all values if there is memory issue by replacing your own logic.
public static void parseCsv() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
// your delimiter
char TOKEN = ',';
// your requirement of storing 10 values for each "line"
int LINE_LIMIT = 10;
// tmp for storing from BufferedReader.read()
int tmp;
// a counter for line break
int lineBreakCount = 0;
// array for storing 10 values, assuming the values of CSV are integers
int[] tempArray = new int[LINE_LIMIT];
// storing tempArray of each line to ArrayList
ArrayList<int[]> lineList = new ArrayList<>();
StringBuilder sb = new StringBuilder();
while((tmp = br.read()) != -1) {
if ((char)tmp == TOKEN) {
if (lineBreakCount == LINE_LIMIT) {
// your logic to handle the current "line" here.
lineList.add(tempArray);
// new "line"
tempArray = new int[LINE_LIMIT];
lineBreakCount = 0;
}
// storing current value from buffer with trim of spaces
tempArray[lineBreakCount] =
Integer.parseInt(sb.toString().trim());
lineBreakCount++;
// clear the buffer
sb.delete(0, sb.length());
}
else {
// add current char from BufferedReader if not delimiter
sb.append((char)tmp);
}
}
br.close();
}
I have the below integers in File :
758 29
206 58
122 89
I have to read these integers in an integer array and then need to store the values in key value pair. Then print the output as :
Position 29 has been initialized to value 758.
Position 89 has been initialized to value 122.
I have tried as of now :
private static Scanner readFile() {
/*
* Your program will prompt for the name of an input file and the read
* and process the data contained in this file. You will use three
* integer arrays, data[], forward[] and backward[] each containing 100
* elements
*/
int data[] = new int[100];
int forward[] = new int[100];
int backward[] = new int[100];
System.out.print("Please enter File Name : ");
#SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String filename = scanner.nextLine();
File inputFile = new File(filename);
Scanner linReader = null;
try {
linReader = new Scanner(new File(filename));
while (linReader.hasNext()) {
String intStringSplit = linReader.nextLine();
String[] line = intStringSplit.split("\t",-1);
data = new int[line.length];
for (int i = 0; i < data.length; i++) {
data[i] = Integer.parseInt(line[i]);
}
System.out.println(data);
}
linReader.close();
} catch (Exception e) {
System.out.println("File Not Found");
}
return linReader;
}
I am not able to figure out how to get the key and value from the read data.
When posting information related to your question it is very important that you provide the data (in file for example) exactly as it is intended in reality so that we can make a more positive determination as to why you are experiencing difficulty with your code.
What you show as an in file data example indicates that each file line (which contains actual data) consists of two specific integer values. The first value being the initialization value and the second being the position value.
There also appears to be a blank line after ever line which contains actual data. This really doesn't matter since the code provided below has a code line to take care of such a thing but it could be the reason as to why you may be having difficulty.
To me, it looks like the delimiter used to separate the two integer values in each file line is indeed a whitespace as #csm_dev has already mentioned within his/her comment but you claim you tried this in your String.split() method and determined it is not a whitespace. If this is truly the case then it will be up to you to determine exactly what that delimiter might be. We couldn't possibly tell you since we don't have access to the real file.
You declare a File object within your provided code but yet nowhere do you utilize it. You may as well delete it since all it's doing is sucking up oxygen as far as I'm concerned.
When using try/catch it's always good practice to catch the proper exceptions which in this case is: IOException. It doesn't hurt to also display the stack trace as well upon an exception since it can solve a lot of your coding problems should an exception occur.
This code should work:
private static Scanner readFile() {
/*
* Your program will prompt for the name of an input file and the read
* and process the data contained in this file. You will use three
* integer arrays, data[], forward[] and backward[] each containing 100
* elements
*/
int data[] = new int[100];
int forward[] = new int[100];
int backward[] = new int[100];
System.out.print("Please enter File Name : ");
Scanner scanner = new Scanner(System.in);
String filename = scanner.nextLine();
File inputFile = new File(filename); // why do you have this. It's doing nothing.
Scanner linReader = null;
try {
linReader = new Scanner(new File(filename));
while (linReader.hasNext()) {
String intStringSplit = linReader.nextLine();
// If the file line is blank then just
// continue to the next file line.
if (intStringSplit.trim().equals("")) { continue; }
// Assuming at least one whitespace is used as
// the data delimiter but what the heck, we'll
// use a regular expression within the split()
// method to handle any number of spaces between
// the integer values.
String[] line = intStringSplit.split("\\s+");
data = new int[line.length];
for (int i = 0; i < line.length; i++) {
data[i] = Integer.parseInt(line[i]);
}
System.out.println("Position " + data[1] +
" has been initialized to value " +
data[0] + ".");
// do whatever else you need to do with the
// data array before reading in the next file
// line......................................
}
linReader.close();
}
catch (IOException ex) {
System.out.println("File Not Found");
ex.printStackTrace();
}
return linReader;
}
I am doing a very basic loop through a file. The file contains a number of entries, however, it seems to break after the 3rd loop which definately contains more than 25 characters. The simple loop is as follows:
public static void organiseFile() throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
String directory = "C:\\Users\\hussainm\\Desktop\\Files\\ex1";
Scanner fileIn = new Scanner(new File(directory + "_temp.txt"));
PrintWriter out = new PrintWriter(directory + "_ordered.txt");
while (fileIn.hasNextLine() == true) {
if (!fileIn.nextLine().isEmpty()) {
lines.add(fileIn.nextLine());
String test = fileIn.nextLine().substring(12, 25);
System.out.println(test);
}
}
I am not sure what the issue is, but it keeps throwing:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 25 at java.lang.String.substring(Unknown
Source) at
fedOrganiser.fedOrganiser.organiseFile(fedOrganiser.java:41) at
fedOrganiser.fedOrganiser.main(fedOrganiser.java:31)
Not sure what its issue is.
File is as follows:
https://www.dropbox.com/s/69h1f8u387zikbp/ex1_temp.txt?dl=0
Every call to nextLine() reads the next line from the stream. It is nextLine(), not hasNextLine(), which advances the stream one line's worth of text. You are reading 3 lines per loop.
When calling nextLine for the first time in a loop, assign it to a variable and refer to that variable for the rest of the loop.
String line = fileIn.nextLine();
if (!line.isEmpty()) {
lines.add(line);
String test = line.substring(12, 25);
System.out.println(test);
}
Incidentally, there is no need to compare a boolean such as what is returned by hasNextLine() to true. Just use the boolean itself, e.g.:
while (fileIn.hasNextLine()) {
You're assuming every line has at least 25 characters in it with the line:
String test = fileIn.nextLine().substring(12, 25);
I'm guessing you have some lines that are shorter or blank.
You'd check String length() before doing substrings.
The call to scanner.nextLine() advances to the next line. You should do it like this, if you are sure that every line has at least 25 characters:
public static void organiseFile() throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
String directory = "C:\\Users\\hussainm\\Desktop\\Files\\ex1";
Scanner fileIn = new Scanner(new File(directory + "_temp.txt"));
PrintWriter out = new PrintWriter(directory + "_ordered.txt");
while (fileIn.hasNextLine()) {
String line = fileIn.nextLine();
if (!line.isEmpty()) {
lines.add(line);
String test = line.substring(12, 25);
System.out.println(test);
}
}
...
}
What I do not understand is what you want to test with line.isEmpty() because this will be always true as long as there are lines. Even a seemingly empty line contains at least a line break.
The exception will be thrown if the line you are parsing is of less than 25 chars long.
Notes
Not sure if your intent is to parse every 3 lines, but
fileIn.nextLine() appear three time. So you are missing one line out of three.
See doc:
Advances this scanner past the current line and returns the input that
was skipped.
Maybe this is what you are trying to do:
Scanner in = null;
PrintWriter out = null;
try {
URL url = this.getClass().getResource("/test_in.txt");
File file = new File(url.getFile());
ArrayList<String> lines = new ArrayList<>();
in = new Scanner(file);
out = new PrintWriter("/test_out.txt");
int lineNumber = 0;
while (in.hasNextLine()) {
String line = in.nextLine();
lineNumber++;
if (line != null && line.trim().length() > 0) {
lines.add(line);
String test = line.substring(12, line.length()<25?line.length():25);
System.out.println(String.format("line# %d: \t\"%s\"", lineNumber, test));
}
}
System.out.println(String.format("last line number: %d", lineNumber));
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
in.close();
out.close();
}
EDIT: For the completeness
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
My code is supposed to read a file line by line, to put each one in a string array named: tuples, (tuples=ligne.split(" "); ) and then add this tuples to an arraylist, one line read = one element of the arraylist.
Unfortunately, my code doesn't work! My buffered reader throws a NullPointerException in my loop!
Error returned in the ExtractFile() method, line 120: tuples= ligne.split(" ");
File file = new File("Department.txt");
BufferedReader in;
PrintWriter out;
String ligne;
int nbCols; //number of metadatas in my file.txt
String metadata[] = new String[nbCols];
String[] tuples = new String[nbCols];//1ere case indique num ligne
ArrayList<String[]> itemset = new ArrayList<String[]>();
public ArrayList ExtractionFile () {
try {
int i = 1;
in = new BufferedReader(new FileReader(file));
while ((ligne = in.readLine()) != null) {
while (ligne.charAt(0) != '1') {
ligne = in.readLine();//browse until line 1 is found
}
tuples = ligne.split(" ");
itemset.add(tuples);
System.out.println(Arrays.toString(itemset.get(0)));
for (i = 2; i < TuplesCount(); i++) {
ligne = in.readLine();
tuples = ligne.split(" ");// THIS LINE THROWS THE EXCEPTION
itemset.add(tuples);
System.out.println(Arrays.toString(itemset.get(i)));
}
}
} catch (IOException es) {
es.printStackTrace();
} catch (ArrayIndexOutOfBoundsException exepti) {
exepti.printStackTrace();
}
return itemset;
}
A NullpointerException tells you that your code
attempts to use null in a case where an object is required. These include:
Calling the instance method of a null object.
...
In your case, you've told us that the error occurs on
tuples = ligne.split(" ");
You're trying to call split on ligne and - given the documentation quote above - that implies that ligne is null at that point.
It's worth noting that it is the second occurence of that line that throws the Exception, so it is not as simple as the file not having a line that starts with 1
Why?
You need to use a debugger or use lots of System.out.println to convince yourself how the value of ligne changes as your code executes and, crucially, where it becomes null.
In your case - we don't know what TuplesCount() returns, but the likelihood is that you keep iterating in that loop beyond the end of the file, so the line immediately before the one that fails
ligne = in.readline();
can return null and you never check for that at that point. Note that conditions for the loops are only checked after each iteration of the loop - so , except for the one right at the top in the condition of the while loop, all the calls to ligne = in.readline(); could return null and are never checked.
The code you provide us is really unreadable and should be formatted to easy debug it. Instead, you can try this simple code that i have tested on my IDE
package example;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class FileSplit {
public static void main(String[] args) {
ArrayList<String[]> phrases = new ArrayList<String[]>();
try{
BufferedReader in = new BufferedReader(new FileReader("Department.txt"));
String s;
while((s = in.readLine()) != null){
String[] words = s.split(" ");
phrases.add(words);
}
}catch(Exception e){
e.printStackTrace();
}
for(int i=0;i<phrases.size();i++){
for(int j=0;j<phrases.get(i).length;j++)
System.out.println(phrases.get(i)[j]);
}
}
}
Are you testing this with a file which doesn't contain a line 1? If so, you run this it will keep executing the inner loop despite the fact that the value is null, resulting in an exception the time after the last line has been read. To fix this change the inner loop to be an if.
Like so:
public ArrayList ExtractionFile() {
try {
int i = 1;
in = new BufferedReader(new FileReader(file));
ligne = in.readLine(); //It can be more readable to seperate the assignment and the condtion
while (ligne != null) {
if (ligne.charAt(0) != '1') {
ligne = in.readLine();//browse until line 1 is found
} else {
tuples = ligne.split(" ");
itemset.add(tuples);
System.out.println(Arrays.toString(itemset.get(0)));
for (i = 2; i < TuplesCount(); i++) {
ligne = in.readLine();
tuples = ligne.split(" ");// CETTE LIGNE SOULEVE L EXCEPTION
itemset.add(tuples);
System.out.println(Arrays.toString(itemset.get(i)));
}
}
}
} catch (IOException es) {
es.printStackTrace();
} catch (ArrayIndexOutOfBoundsException exepti) {
exepti.printStackTrace();
}
return itemset;
}
private int TuplesCount() {
return Arrays.asList(tuples).size();
}
public static void main(String[] args){
Main main = new Main();
main.ExtractionFile();
}
}
As per some of the points above, please do format your code - it makes it easier to get help! Also, read up on breakpoints!