I have a .txt file containing document information (For 1400 documents). Each document has an ID, title, author, area and abstract. A sample looks like this:
.I 1
.T
experimental investigation of the aerodynamics of a
wing in a slipstream .
.A
brenckman,m.
.B
j. ae. scs. 25, 1958, 324.
.W
experimental investigation of the aerodynamics of a
wing in a slipstream .
[...]
the specific configuration of the experiment .
I want to put each of these into 5 arrays dedicated to each category. I'm having trouble inserting the title and abstract into a single array position, can anyone tell me what's wrong with this code? What I am trying to do is insert the text lines into position x after a ".T" is read and stop when it finds a ".A", when it happens, increase position by 1 for it to fill the next position
try{
collection = new File (File location);
fr = new FileReader (collection);
br = new BufferedReader(fr);
String numDoc = " ";
int pos = 0;
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
Title[pos] = Title[pos] + numDoc;
pos++;
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
The goal is to have all the information within a single line of String. Any help would be greatly appreciated.
In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.
A code walkthrough is always helpful. In the future, you can probably use breakpoints, but I think I know why you're getting what I assume is a Null Pointer Exception.
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
Outside, everything looks good, In this loop is where the things start going bonkers.
Title[pos] = Title[pos] + numDoc;
With your provided input, we would set:
Title[0]
as Title[0] + "experimental investigation of the aerodynamics of a"
This works only if Title[0] exists, which I don't assume it has been initialized, yet. We'll address that issue first by correctly detecting for a null array value. This would either be a compiler error about something not being initialized or a run-time null pointer exception. Off the top of my head, I want to say compiler error.
So anyways, we'll address dealing with null Title[pos].
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
if(Title[pos] != null) {
Title[pos] = Title[pos] + numDoc;
}
else {
Title[pos] = numDoc;
}
pos++;
}
}
}
When we do another walkthrough, we'll get the following array values
Title[0]=experimental investigation of the aerodynamics of a
Title[1]=wing in a slipstream .
If this intended, then this is fine. If you wanted the titles together, then you move the pos++
out the while loop.
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".T")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".A")){
if(Title[pos] != null) {
Title[pos] = Title[pos] + " " + numDoc; // add a space between lines
}
else {
Title[pos] = numDoc;
}
}
pos++;
}
}
Then we get:
Title[0]=experimental investigation of the aerodynamics of a wing in a slipstream .
You may want to trim your inputs, but this should cover both of the potential errors that I can see.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With