• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What's wrong with this code?

Howard

Lifer
public class Mergesort
{
public static void main (String [] args) throws IOException
{
BufferedReader input1, input2;
PrintWriter output;

String line;
Vector olddata = new Vector (1, 1);
Vector newdata = new Vector (1, 1);
Vector merged = new Vector (1, 1);

int index = 0;
int length = 0;

input1 = new BufferedReader (new FileReader ("phone.txt"));
line = input1.readLine ();
while (line != null)
{
olddata.addElement (line);
line = input1.readLine ();
length++;
}
input1.close ();
length = 0;
input2 = new BufferedReader (new FileReader ("customer.txt"));
line = input2.readLine ();
while (line != null)
{
newdata.addElement (line);
line = input2.readLine ();
length++;
}
input2.close ();
for (int i = 0 ; i < olddata.size () ; i++)
{
merged.addElement (olddata.elementAt (i));
}
for (int i = 0 ; i < newdata.size () ; i++)
{
merged.addElement (newdata.elementAt (i));
}
for (int x = 0 ; x < merged.size () ; x++)
{
int smallest = -1;
for (int y = x + 1 ; y < merged.size () ; y++)
{
if ((((String) merged.elementAt (y)).toLowerCase ()).compareTo (((String) merged.elementAt (x)).toLowerCase ()) < 0)
{
smallest = y;
}
}
if (smallest != -1)
{
merged.insertElementAt ((String) merged.elementAt (smallest), x);
merged.removeElementAt (smallest + 1);
merged.trimToSize ();
}
}
output = new PrintWriter (new FileWriter ("current.txt"));
for (int i = 0 ; i < merged.size () ; i++)
{
output.println (merged.elementAt (i));
}
output.close ();
}
}

Basically I read two text files into two vectors, merge the vectors together, and sort them. I think the sorting algorithm is wrong, although for my last program it worked (I transplanted it). Anybody care to help?

Hope the formatting comes out OK...
 
what's the error and what's the compiler? Don't make me copy and paste this and do all your work for you
rolleye.gif
 
This otta fix it:

in length = 12;

input1 = length++ d!ck
Vector merged => (_(|)_) C==(_)_)

merged.insertElementAt ((String) merged.elementAt (smallest), x); = uh Uh UH AHHH!

output.close (|);

😉
 
You are doing a Mergesort correct?

That involves a recursive call.

In general, if you have two Vectors sorted in ascending order. You simply take the smallest of the two front elements and put it into the merged Vector. Bam, you're done.

The problem is that you have to have two _sorted_ Vectors. But, that's why you call Mergesort recursively until you do have two sorted Vectors. I don't really see that in your code.
 
Originally posted by: cheapbidder01
This otta fix it:

in length = 12;

input1 = length++ d!ck
Vector merged => (_(|)_) C==(_)_)

merged.insertElementAt ((String) merged.elementAt (smallest), x); = uh Uh UH AHHH!

output.close (|);

😉

LOL
 
Back
Top