• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading & Writing to text file using ArrayList incorrectly

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have been able to store two strings (M44110|T33010, M92603|T10350) using ArrayList<String>, which represent the result of some patient tests. Nevertheless, the order of these results have been stored correctly.

E.g. currently stored in file Snomed-Codes as -

[M44110|T33010, M92603|T10350][M44110|T33010, M92603|T10350]

Should be stored and printed out as -

M44110|T33010
M92603|T10350

Below is the detail of the source code of this program:

import java.io.*;
import java.io.IOException;
import java.lang.String;
import java.util.regex.*;
import java.util.ArrayList;

public class FindSnomedCode {

static ArrayList<String> allRecords = new ArrayList<String>();

static public ArrayList<String> getContents(File existingFile) {
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
int line_number = 0;
int number_of_records = 0;
String snomedCodes = null;
ArrayList<String> complete_records = new ArrayList<String>();

try {
input = new BufferedReader( new FileReader(existingFile) );
String current_line = null;
while (( current_line = input.readLine()) != null) {

// Create a pattern to match for the "\"
Pattern p = Pattern.compile("\\\\");
// Create a matcher with an input string
Matcher m = p.matcher(current_line);
boolean beginning_of_record = m.find();
if (beginning_of_record) {
line_number = 0;
number_of_records++;
} else {
line_number++;
}

if ( (line_number == 2) && (current_line.length() != 0) ) {
snomedCodes = current_line;
System.out.println(snomedCodes);
complete_records.add(current_line);
}
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
return complete_records;
}


static public void setContents(File reformatFile, ArrayList<String> snomedCodes)
throws FileNotFoundException, IOException {

Writer output = null;
try {
output = new BufferedWriter( new FileWriter(reformatFile) );
for (String index : snomedCodes) {
output.write( snomedCodes.toString() );
}
}
finally {
if (output != null) output.close();
}
}

static public void printContents(File existingFile) {
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
int line_number = 0;
int number_of_records = 0;
String snomedCodes = null;
try {
input = new BufferedReader( new FileReader(existingFile) );
String current_line = null;
while (( current_line = input.readLine()) != null)
System.out.println(current_line);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}

public static void main (String args[]) throws IOException {
File currentFile = new File("D:\\dummy_patient.txt");
allRecords = getContents(currentFile);

File snomedFile = new File( "D:\\Snomed-Codes.txt");
setContents(snomedFile, allRecords);
printContents(snomedFile);
}
}


There are 4 patient records in the dummy_patient.txt file but only the 1st (M44110|T33010) & 3rd (M92603|T10350) records have results in them. The 2nd & 4th records have blank results.

Lastly, could someone explain to me the difference between java.util.List & java.util.ArrayList?

I am running Netbeans 5.0, jdk1.5.0_09 on Windows XP, SP2.

Many thanks,

Netbeans Fan.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Helen Tran:
Lastly, could someone explain to me the difference between java.util.List & java.util.ArrayList?


One is an interface and the other is a class that is a concrete implementation of that interface.

It's not entirely clear to me what you're trying to do. In the future please try to post code between code tags.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the problem is with storing the results I would suspect that



should be



And your naming of the variable "index" is suspect since it's not an index, it's the current element in the iteration. The above is equivalent to:



It does the exact same thing, it's just cleaner and clearer to do it with the enhanced for loop.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed you seem to want each on a new line too.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic