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

Recommended Answers

All 11 Replies

In the class: ClassRecord have you implemented (overridden) the method toString() ? If not then when you call this:

pw.println(theList.toString());;

since you have no toString() method you call the toString() method of the super class.
Yes, ClassRecord has a super class, the Object class. In final what you call is the toString() method of the Object class.
Better write in the ClassRecord a toString() method returning what you want to write in the file

oh sorry forgot to mention that the toString method has been defined in another class called ClassID which follows below. Should i still try and implement a toString in the classRecord though?

public class ClassID
{
private int IDnum, ACnum, Transmode;
private String Name;
private Integer time;

public ClassID()
{
Name= "noname";
IDnum= 0000;
ACnum= 0000;
time= 0000;
Transmode= 0;
}

public ClassID(String inName, int inIDnum, int inACnum, Integer intime, int inTransmode)
{

Name= inName;
IDnum= inIDnum;
ACnum= inACnum;
time= intime;
Transmode= inTransmode;

}
public ClassID ( ClassID inClassID)
{
Name= inClassID.getName();
IDnum= inClassID.getIDnum();
ACnum= inClassID.getACnum();
time= inClassID.getTime();
Transmode= inClassID.getTransmode();
}

public String getName()
{
return new String( Name);
}

public int getIDnum()
{
return IDnum;
}

public int getACnum()
{
return ACnum;
}

public Integer getTime()
{
return time;
}

public int getTransmode()
{
return Transmode;             
}

public String toString()
{
return new String( "Employee name is "+Name + "   ID number is " +IDnum + "   Access Control number is " +ACnum + "   The Time is  " +time +   "          Transmission mode was " +Transmode);
}

public void setAll( int inIDnum, int inACnum, int inTransmode, String inName)
{
Name= inName;
IDnum= inIDnum;
ACnum= inACnum;
Transmode= inTransmode;
}

public void setTime (Integer intime)
{
time= intime;
}
pw.println(theList.toString());;

You call the toString() of class ClassRecord, not ClassID.
I see that you have a for-loop but you call the same thing:

for (int i = 0; i < count; i++)
{
pw.println(theList.toString());
}

You should be getting in each loop the ClassID item and writing the toString() of that:

Add a getClassID(int index) method in ClassRecord
Add a getCount() method in ClassRecord : will return the count variable

And have count being the size of the array

count = theList.getCount();
for (int i = 0; i < count; i++)
{
ClassID clId = theList.getClassID(i);
pw.println(clId.toString());
}

thanks alot for your help, unfortunately i cant try this out till tomorrow now, but i will let you know how it goes. I understand what your saying though and it makes sense.

this is probably a bad question but this:

public ClassRecord getClassID(int index)
	{
	return ClassID[index];
	}

is giving me a cant find symbol ClassID error, is that what its supposed to look like

ok i got it to compile but im not sure i did it right as im still getting the same funny output in my newly created file.

hers the getClassID method

public ClassID getClassID(int index)
	{
	return new ClassID();
	}

ok i have made a few changes, and information is now printed to the file however:

this

public ClassID getClassID(int index)
	{
	return  new ClassID ();
	}

and this

count = theList.getCount();
	for (int i = 0; i < count; i++)
		{
		ClassID clId = theList.getClassID(i);
		pw.println(clId.toString());
		}

simply calls the dafault constructor of ClassID:

public ClassID()
{
Name= "noname";
IDnum= 0000;
ACnum= 0000;
time= 0000;
Transmode= 0;
}

thus the output in the file is simply:
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
Employee name is noname ID number is 0 Access Control number is 0 The Time is 0 Transmission mode was 0
etc, etc

what am i missing here?

this is probably a bad question but this:

public ClassRecord getClassID(int index)
	{
	return ClassID[index];
	}

is giving me a cant find symbol ClassID error, is that what its supposed to look like

At the above the error is this:
return ClassID[index]; and
public ClassRecord getClassID(int index)

You got close with this:

public ClassID getClassID(int index)
	{
	return new ClassID();
	}

Use this:

public ClassID getClassID(int index)
	{
	return ............[index]; //add what is missing at the dots. What do you need to return from the class ClassRecord? What the for-loop in which this is used does?
	}

of course!!

public ClassID getClassID(int index)
	{
	return  theList[index];
	}

I can not thank you enough :)

Does the whole program work now?

yep all good.

Now on to my next job....creating a stack Class for ClassID and pushing the contents of my file (you helped to create) onto said stack.

Should be fun haha.

tha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.