Hey everyone, im working on a project for uni.
The question wants me to read from a text file, place the info into an array, sort the information in that array and write it to a new file with the sorted information. So far i have been able to read from a file and place it in to an array, then sort the array. However im having trouble writing the newly sorted array into a new file.
Before i show the code ill explain the problem. I cant figure out how to actually write the information to the file. ie i cant call my print method within the print writer method as it returns a void.
Here is the class that reads the file, passes into the array, sorts and then writes to a new file in progress:
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
ClassRecord theList = new ClassRecord();
ReadFile rf = new ReadFile();
rf.ReadFile(theList);
}
public void ReadFile(ClassRecord theList){
try
{
FileReader fr = new FileReader("info.txt");
BufferedReader br = new BufferedReader(fr);
String name = br.readLine();
while(name != null)
{
int IDnum= Integer.parseInt(br.readLine());
int ACnum= Integer.parseInt(br.readLine());
Integer time= Integer.parseInt(br.readLine());
int Transmode= Integer.parseInt(br.readLine());
theList.addtransaction(new ClassID (name,IDnum,ACnum,time,Transmode));
name = br.readLine();
}
theList.insertionSort();
br.close();
System.out.println("");
System.out.println("SORTED ACCORDING TO TIME");
System.out.println("");
theList.print();
}
catch (IOException e)
{
e.printStackTrace();
}
FileOutputStream outStr;
int count;
PrintWriter pw;
String fileName = "sortedinfo.txt";
try
{
outStr = new FileOutputStream (fileName);
pw = new PrintWriter(outStr, true);
count = 50;
for (int i = 0; i < count; i++)
{
pw.println(theList.toString());; // This is the section not working it gives funny output in the printed file.
}
pw.close();
}
catch (IOException e)
{
System.out.println("Error " + fileName);
}
}
Here is the class record class:
import java.io.*;
public class ClassRecord
{
private ClassID theList[];
private int count, max;
public ClassRecord()
{
count = 0;
max = 50;
theList = new ClassID[max];
}
public ClassRecord (int inSize)
{
count = 0;
if( inSize >0)
max = inSize;
else
max = 50;
theList = new ClassID[max];
}
public void addtransaction (ClassID inClassID)
{
if ( count < max )
{
theList[count] = new ClassID(inClassID);
count++;
}
}
public ClassID find (ClassID searchName)
{
int j;
for(j=0; j<max; j++)
if (theList[j].getName().equals(searchName) )
break;
if (j == max)
return null;
else
return theList[j];
}
public void print()
{
for (int i = 0; i < count; i++)
System.out.println(theList[i].toString());
}
public void insertionSort()
{
int in, out;
for (out=1; out<max; out++)
{
ClassID temp = theList[out];
in = out;
while(in>0 && theList[in-1].getTime().compareTo(temp.getTime())>0)
{
theList[in] = theList[in-1];
--in;
}
theList[in] = temp;
}
}
}
Any help is greatly appreciated as it is driving me crazy!!
PS. This is the output i get when the new file is printed
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30
ClassRecord@923e30