Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class dataReader {
#SuppressWarnings("rawtypes")
public static void main(String[] args) throws Exception {
String splitBy =",";
BufferedReader br = new BufferedReader(new FileReader("C:/practice/testData.csv"));
String line = br.readLine();
int counter = 0;
while ((line = br.readLine()) != null){
counter++;
String[] b = line.split(splitBy);
for (int x = 0; x < b.length; x++){
System.out.println(b[x]);
}
}
System.out.println(counter);
br.close();
}
}
When I run it, it goes through the CSV and displays some output but it starts with product ID 4000+. It basically only outputs results for the last thousand rows in the CSV. I'm wrangling with a really ugly CSV file to try to get some useful data out of it so that I can write all of it into a new database later on. I'd appreciate any tips.
As in the comments, the issue is that System.out.println prints to your console. The console will have a limit to the number of lines it can display, which is why you see the last ~1000 lines.
At a frist glance at your code, you skip the first line by assigning a readline() to your string line
String line = br.readLine();
You should change that as you read the first line and than enter the loop which will read the 2nd line before any operations on the first line took place.
Try something like
String line = "";
Your code won't handle more complicated CSVs, eg a row:
"Robert, John", 25, "Chicago, IL"
is not going to get parsed correctly. Just splitting on ',' is going to separate "Robert, John" into multiple cells though it should be just 1 cell.
The point is, you shouldn't be writing a CSV parser. I highly recommend go downloading the Apache Commons CSV library and using that! Reading CSVs is a solved problem; why reinvent the wheel?
Without seeing the structure of the file I can only speculate, but this line: String line = br.readLine(); needs to be changed to String line = "" or String line = null. As it currently stands, you are discarding your first line.
Related
I've been creating a game in Java for a while and I used to write all the in-game texts directly in my code like this:
String text001 = "You're in the castle.\n\nWhere do you go next?"
But recently I decided to write all the in-game texts in a text file and tried to let the program read them and put them into a String array since the amount of the texts has increased a lot and it made my code incredibly long. The reading went well except one thing. I've inserted line break codes in dialogues and although the code worked properly when I wrote it directly in my code, they are no longer recognized as line break code when I try to read them from a text file.
It is supposed to be displayed as:
You're in the castle.
Where do you go next?
But now it is displayed as:
You're in the castle.\n\nWhere do you go next?
The code doesn't recognize "\n" as line break code any more.
Here's the code :
import java.io.File;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
Scanner sc;
StringTokenizer token;
String line;
int lineNumber = 1;
String id[] = new String[100];
String text[] = new String[100];
try {
sc = new Scanner(new File("sample.txt"));
while ((line = sc.nextLine()) != null) {
token = new StringTokenizer(line, "|");
while (token.hasMoreTokens()) {
id[lineNumber] = token.nextToken();
text[lineNumber] = token.nextToken();
lineNumber++;
}
}
} catch (Exception e) {
}
System.out.println(text[1]);
String text001 = "You're in the castle.\n\nWhere do you go next?";
System.out.println(text001);
}
}
And this is the content of the text file:
castle|You're in the castle.\n\nWhere do you go next?
inn|You're in the inn. \n\nWhere do you go next?
I would be grateful if anyone tells me how to fix this. Thank you.
Just use
text[lineNumber] = token.nextToken().replace("\\n", "\n");
There is nothing inherently special about \n in a text file. It is just a \, followed by a \n.
It is only in Java (or other languages) which define that this sequence of characters - in a char or string literal - should be interpreted as a 0x0a (ASCII newline) character.
So, you can replace the character sequence with the one you want it to be interpreted as.
package com.testing;
import java.io.BufferedReader;
import java.io.FileReader;
public class CsvParser {
public static void main(String[] args) {
String csvFile = "D:/code-home/SentimentAnalysis/test_data/Sentiment Analysis Dataset.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = "\t"; // data is in format splitted by tab
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] tweet = line.split(cvsSplitBy);
System.out.println(tweet[1]);
System.out.println(tweet[3]);
}
}
}
The program's purpose is to parse the CSV format. I have used bufferRead method.
When I go to compile the program, it works fine. When I run the program,output is printed but there is a exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.testing.CsvParser.main(CsvParser.java:34)
First of all: Arrays in Java are Zero-indexed, which means that the first element in an array is not selected by array[1] but by array[0]. Since your OutOufBoundsException is fired at the index 1, your array has at most one element in it (you shoulds check for the size of the array before accessing it). Because you are trying to access the index 3 (fourth element in Java) in the very next line i suspect you expect at least 3 elements in each line. Since there is at most one element you seem to either be using the wrong splitcharacter or your file is not formatted as you expect it to be. I hope this helps you. Kind regards
I'm trying to read a file line by line, but every time I run my program I get a NullPointerException at the line spaceIndex = line.indexOf(" "); which obviously means that line is null. HOWEVER. I know for a fact that the file I'm using has exactly 7 lines (even if I print the value of numLines, I get the value 7. And yet I still get a nullpointerexception when I try to read a line into my string.
// File file = some File I take in after clicking a JButton
Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), charset)) {
String line = "";
int spaceIndex;
int numLines = 0;
while(reader.readLine()!=null) numLines++;
for(int i = 0; i<numLines; i++) {
line = reader.readLine();
spaceIndex = line.indexOf(" ");
System.out.println(spaceIndex);
}
PS: (I'm not actually using this code to print the index of the space, I replaced the code in my loop since there's a lot of it and it would make it longer to read)
If i'm going about reading the lines the wrong way, it would be great if someone could suggest another way, since so far every way I've tried gives me the same exception. Thanks.
By the time you start your for loop, the reader is already at the end of the file
(from the while loop).
Therefore, readLine() will always return null.
You should get rid of the for loop and do all of your work in the while loop as you first read the file.
You have two options.
First, you could read number of lines from a file this way:
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("File1")));
lnr.skip(Long.MAX_VALUE);
System.out.println(lnr.getLineNumber());
Then read the file right after:
while((line = reader.readLine())!=null)
{
spaceIndex = line.indexOf(" ");
System.out.println(spaceIndex);
}
This first option is an alternative (and in my my opinion, cooler) way of doing this.
Second option (and probably the more sensible) is to do it all at once in the while loop:
while((line = reader.readLine())!=null)
{
numLines++;
spaceIndex = line.indexOf(" ");
System.out.println(spaceIndex);
}
I'm writing this code to look inside a txt file and find me a string that the user gave as input. My txt file contains the lines as such (this info will be important later):
first line - blank.
second line - idan
third line - yosi
now, if the user inputs "idan" as the user (without the "") the code will find it. If the user puts in "yosi" it wont find it. It's like my code is reading only the second line. I'm new in programming and this is just a practice for me to learn how to read and write to files, please be patient with me.
here is the code (there is a catch and also the else statement but they where left off for length reasons):
//Search for the specific profile inside.
try{
BufferedReader br = new BufferedReader(new FileReader("d:\\profile.txt"));
System.out.println("Searching for your Profile...");
int linecount = 0;
String line;
while (br.readLine() !=null){
linecount++;
if(userName.contentEquals(br.readLine())){
System.out.println("Found, " + userName + " profile!");
break;
}
else{
}
The problem is this:
*if(userName.contentEquals(br.readLine())){*
you are reading an additional line. You will find it reads every other line with your implementation. That is line 2,4,6,etc
The problem is in the following place:
if(userName.contentEquals(br.readLine()))
You don't need to read it again because you have already read it in the while loop:
while (br.readLine() !=null)
So, you basically read line1 (do nothing with it), then read line2 (do something with it) and the process starts over.
You want to do something like
...
String line;
while((line = br.readLine()) != null) {
...
}
Every call to BufferedReader.readLine() reads the next available line from the file. Since you read one line in the while statement and read the next line for the if statement, you're only checking the even numbered lines.
Is there a way to read two lines of a csv file at a time in Java?
I can read one at a time using Scanner (it has to be done like this):
String line = input.nextLine();
String[] nline = line.split ("[,]");
......
Here is some sample data and a short explanation. I need these read two at a time so I can can go about my other processing.
the first line that starts with "Create" creates a person
the second line "action" is the action of the created person
create,Mr. Jones,blah,blah
action,1,3
create,Mrs.Smith,blah,blah
action,4,10
....
Thanks in advance.
If you're looking to parse CSV files in Java I'd avoid line split via the string.split() method. You can run into issues if your field contains commas. For Java I'd recommend opencsv to parse the data. Similar to using the scanner you can read it in line by line, or slurp the entire file if it's not too large, and just iterate over the list to items at a time.
CSVReader reader = ...
String[] firstLine; // fields from first line
String[] secondLine; // fields from second line
while ((firstLine = reader.next()) != null && (secondLine = reader.next()) != null) {
// do something with two lines
}
Or
CSVReader reader = ...
List<String[]> allLines = reader.readAll();
// TODO: validate we have an even number of lines
for (int i = 0; i < allLines.size(); i += 2) {
String[] firstLine = allLines.get(i);
String[] secondLine = allLines.get(i+1);
// do something with two lines
}
String line = input.nextLine() + input.nextLine();
Cory's answer is good but for your next part to work
String[] nline = line.split ("[,]");
......
You need to add the comma in there
String line = input.nextLine() + "," + input.nextLine();