The first time you're reading the line without processing it in the while loop, then you're reading it again but this time you're processing it. readLine() method reads a line and displaces the reader-pointer to the next line in the file. Hence, every time you use this method, the pointer will be incremented by one pointing to the next line.
This:
while ((newLine = br.readLine()) != null) {
newLine = br.readLine();
System.out.println(newLine);
lines.add(newLine);
}
Should be changed to this:
while ((newLine = br.readLine()) != null) {
System.out.println(newLine);
lines.add(newLine);
}
Hence reading a line and processing it, without reading another line and then processing.
Answer from GingerHead on Stack OverflowThe first time you're reading the line without processing it in the while loop, then you're reading it again but this time you're processing it. readLine() method reads a line and displaces the reader-pointer to the next line in the file. Hence, every time you use this method, the pointer will be incremented by one pointing to the next line.
This:
while ((newLine = br.readLine()) != null) {
newLine = br.readLine();
System.out.println(newLine);
lines.add(newLine);
}
Should be changed to this:
while ((newLine = br.readLine()) != null) {
System.out.println(newLine);
lines.add(newLine);
}
Hence reading a line and processing it, without reading another line and then processing.
You need to remove the first line in a loop body
newLine = br.readLine();
Videos
Ok im trying to read from a csv file and some how my directory can't find it can someone help. The Cvs file is in my project src file which I have named Files as well. Heres the code: import java.io.BufferedReader;
import java.io.FileReader; import java.io.IOException; import java.io.*; public class Operrations {
public static void main(String[] args) throws Exception {
String File = "Files\\Crimes.csv";
BufferedReader reader = null;
String line = "";
try {
reader = new BufferedReader(new FileReader(File));
while((line = reader.readLine()) !=null);
String[] row = line.split(",");
for(String index: row){
System.out.printf("%-10", index);
}
System.out.println();
}
catch (Exception e){
e.printStackTrace();
}
finally {
try{
reader.close();
} catch(IOException e){
e.printStackTrace();
}
}
}}
The following code will read only the first 100 lines of the file and extract the values into a list.
java.nio.file.Path path = java.nio.file.Paths.get(str);
try {
java.util.List<String> values = java.nio.file.Files.lines(path)
.limit(100)
.filter(line -> line.matches("\\$\\$[A-Z]+\\$\\$ [0-9A-Z]*$"))
.map(line -> {
String[] words = line.split(" ");
return words.length == 2 ? words[1] : "";
})
.collect(java.util.stream.Collectors.toList());
System.out.println(values);
}
catch (java.io.IOException xIo) {
xIo.printStackTrace();
}
According to the sample file in your question, the above code will create the following list.
[JOHN, CA, SF, XYZ, , 25, CATEGORY, ]
If you want a Map instead of a List where the Map key is the value between the double $ characters and the Map value is the part after the space, then
Function<String, String> keyMapper = line -> {
String[] parts = line.split(" ");
return parts[0].substring(2, parts[0].length() - 2);
};
Function<String, String> valueMapper = line -> {
String[] parts = line.split(" ");
if (parts.length > 1) {
return parts[1];
}
else {
return "";
}
};
Path path = Paths.get(str);
try {
Map<String, String> map = Files.lines(path)
.limit(100)
.filter(line -> line.matches("\\$\\$[A-Z]+\\$\\$ [0-9A-Z]*$"))
.collect(Collectors.toMap(keyMapper, valueMapper));
System.out.println(map);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
This will create the following Map
{GROUP=CATEGORY, WEATHER=, CITY=SF, STATE=CA, TIME=, NAME=JOHN, REGION=XYZ, AGE=25}
You could use regex here to both detect the name line:
int n = 100; // Max lines
String line;
try (BufferedReader br = new BufferedReader(new FileReader(str))) {
while ((line = br.readLine()) != null && i++ < n) {
if (line.matches("\\$\\$NAME\\$\|$.$")) {
System.out.println(line.split(" ")[1]);
}
}
}