Article View: borland.public.jbuilder.java.language
Article #8431Re: Having problems with a sort...
From: Owen Green
Date: Wed, 21 Mar 2001 14:54
Date: Wed, 21 Mar 2001 14:54
112 lines
4614 bytes
4614 bytes
> Thanks for taking an interest. I would be grateful if you could figure
> it out, it's been killing me for a while now.
This seems to be working - the problem seemed to be that there was no
provision for strings that that had nothing before them in the ordered
ist - it would just skip out of the while loop with the index too low and
place the string above where it should be. That was why in the first place
the first string got pushed to the bottom. The, fairly inelegant, way I've
got around this is to test after the while loop for the marker string being
null, signifying that it went all the way through the file without finding a
place to insert; if this is the case I increment the index count (which will
be beyond the last line of the file). This obviously has the effect that the
while loop in the InsertString method won't pick up that index, because it
never reaches it - therefore the same thing is done again; the comparison
string is tested for null to make sure we went all the way through, and
(just to make sure) the index is compared against i to make sure it really
is beyond the end of the file.
It seems a bit klugdy, so you may be able to think of a more elegant
solution.
hth
Owen
public static void makeIndices() throws IOException
{
BufferedReader inputFile = new BufferedReader(new
FileReader("recordCode.tmp"));
String myDataString = new String(inputFile.readLine());
BufferedWriter dummyIndexFile = new BufferedWriter(new FileWriter(new
File("recordCode.idx")));
dummyIndexFile.write(myDataString);
dummyIndexFile.close();
while((myDataString=inputFile.readLine()) != null)
{
BufferedReader indexFile = new BufferedReader(new
FileReader("recordCode.idx"));
String myIndexCompareString = new String("");
int indexLineAt = 0;
while((myIndexCompareString = indexFile.readLine()) != null)
{
indexLineAt++;
if(myDataString.compareTo(myIndexCompareString) < 0)
{
break;
}
}
indexFile.close();
if (myIndexCompareString == null) indexLineAt++;
insertStringInFile("recordCode.idx", indexLineAt, myDataString);
}
inputFile.close();
} // end of makeIndices method.
public static void insertStringInFile(String inFilename,
int lineno,
String lineToBeInserted)
throws IOException
{
// temp file
File outFile = new File("$$$$$$$$.tmp");
// input
File inFile = new File(inFilename);
FileInputStream fis = new FileInputStream(inFile);
BufferedReader in = new BufferedReader (new InputStreamReader(fis));
// output
FileOutputStream fos = new FileOutputStream(outFile);
PrintWriter out = new PrintWriter(fos);
String thisLine = "";
int i = 1;
while ((thisLine = in.readLine()) != null)
{
if(i == lineno)
{
System.err.println("\n\n" + lineToBeInserted + " @ line " +
String.valueOf(lineno) +"\n");
// System.out.println("\nInsert *_" + lineToBeInserted);
out.println(lineToBeInserted);
}
out.println(thisLine);
System.out.println("\nAlready line *_" + thisLine);
i++;
}
if(thisLine==null && lineno >= i) out.println(lineToBeInserted);
out.flush();
out.close();
in.close();
inFile.delete();
outFile.renameTo(inFile);
} // end of insertLine method.
Technical Details
Message-ID:
<3ab8c09f$1_2@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!128.230.129.106!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!213.86.7.136References:
<3ab4c4e3.444852043@newsgroups.borland.com> <3ab62e22$1_2@dnews> <3ab63e8d.541533744@news.esat.net> <3ab6536e_1@dnews> <3ab88dce.692894920@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,91a554b109e4b985 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-23 03:57:35 PST From: "Owen Green" <owen@norobots.unique.com> Newsgroups: borland.public.jbuilder.java.language References: <3ab4c4e3.444852043@newsgroups.borland.com> <3ab62e22$1_2@dnews> <3ab63e8d.541533744@news.esat.net> <3ab6536e_1@dnews> <3ab88dce.692894920@newsgroups.borland.com> Subject: Re: Having problems with a sort... Date: Wed, 21 Mar 2001 14:54:11 -0000 Lines: 114 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 213.86.7.136 Message-ID: <3ab8c09f$1_2@dnews> X-Trace: 21 Mar 2001 06:54:23 -0800, 213.86.7.136 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!128.230.129.106!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!213.86.7.136