codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  sorting and Linked list  gosi at 13:22 on Tuesday, December 27, 2005
 

Hi. How can I sort the data written to my output file so that the lowest a-value is first, second lowest is next and etc.

If the CP.txt is like this,

nr a b
1 10 10
2 5 10
3 15 10

how can I get the output: nr a b
2 5 10
1 10 10
3 15 10

Also alternative, is it possible to use Linked list to solve this problem?


here´s my code:

import java.awt.*;
import java.util.ArrayList;
import java.io.*;
import javax.swing.*;

public class CP extends JPanel {

String file;
FileWriter fw;
BufferedWriter bw;
PrintWriter outFile;

int[] nrVals;
int[] aVals;
int[] bVals;

private ArrayList arrayInfo = new ArrayList();

public CP()throws IOException
{
file = "/forum/results.txt";
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
openCommand(new File("/forum/c/cp.txt"));

}

public void openCommand(File file)
{

arrayInfo.clear();
StringBuffer sb = new StringBuffer();
try
{
BufferedReader reader = new BufferedReader(
new FileReader(file));
// read in all the data from file
String line = "";
while((line = reader.readLine()) != null)
sb.append(line + "/index.html");
reader.close();
}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Can´t read the file", "Error",
JOptionPane.ERROR_MESSAGE);
file=null;
}
// recover and parse data
String[] lines = sb.toString().split("/index.html");
// now that we know the number of lines we can
// initialize the arrays that will hold the parsed data
nrVals = new int[lines.length];
aVals = new int[lines.length];
bVals = new int[lines.length];
// just for fun let's pass on the data to instances
// of a data store class - OtherClass
OtherClass[] otherClasses = new OtherClass[lines.length];
for(int j = 0; j < lines.length; j++)
{
String[] vals = lines[j].split("\\s");

int nr = Integer.parseInt(vals[0]);
int a = Integer.parseInt(vals[1]);
int b = Integer.parseInt(vals[2]);
// assign j_th element of our three arrays



nrVals[j] = nr;
aVals[j] = a;
bVals[j] = b;
//weights[j] = w;
// assign j_th element of our OtherClass array
//otherClasses[j] = new OtherClass(a, b, w);
otherClasses[j] = new OtherClass(nr,a, b);
outFile.println("nr:"+nr +" a:" +a);

}
// print out our three arrays; for j2se 1.5 see:
// java.util.Arrays.toString(int[] a)
print(nrVals, "nr");
print(aVals, "aVals");
print(bVals, "bVals");


for(int j = 0; j < otherClasses.length; j++);
outFile.close();
}






private static void print(int[] array, String arrayName)
{
System.out.print(arrayName + " = [");
for(int j = 0; j < array.length; j++)
{
System.out.print(array[j]);
if(j < array.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}

public static void main(String[] args)throws IOException
{
new CP();
}

}
class OtherClass
{
int nr = 0;
Point point = new Point();

public OtherClass(int nr, int a, int b)
{
this.nr=nr;
point.x = a;
point.y = b;
}

public Point getPoint()
{
return point;
}

public int getNr()
{
return nr;
}


}



  Re: sorting and Linked list  crwood at 18:25 on Tuesday, December 27, 2005
 

is it possible to use Linked list to solve this problem?
The api is a good place to look for information to answer such questions. Start with the Overview. In the lower left frame scroll down to the LinkedList class listing, click on the link and the class api will load into the main frame. The discussion section tells what LinkedList is especially useful for. You might also be interested in the TreeMap api. For more on what's possible with Collections see Trail: Collections in the tutorial.

import java.io.*;
import javax.swing.JOptionPane;

public class CP{

public CP()
{
openCommand(new File("/forum/cp.txt"));
}

public void openCommand(File file)
{
StringBuffer sb = new StringBuffer();
try
{
BufferedReader reader = new BufferedReader(
new FileReader(file));
String line = "";
while((line = reader.readLine()) != null)
sb.append(line + "/index.html");
reader.close();
file = new File("/forum/results.txt");
PrintWriter outFile = new PrintWriter(
new BufferedWriter(
new FileWriter(file)));
String[] lines = sb.toString().split("/index.html");
int[] lineNums = new int[lines.length];
int[] aVals = new int[lines.length];
int[] bVals = new int[lines.length];
for(int j = 0; j < lines.length; j++)
{
String[] vals = lines[j].split("\\s");
lineNums[j] = Integer.parseInt(vals[0]);
aVals[j] = Integer.parseInt(vals[1]);
bVals[j] = Integer.parseInt(vals[2]);
}
sort(aVals, lineNums, bVals);
for(int j = 0; j < lineNums.length; j++)
outFile.println("nr:" + lineNums[j] + " a:" + aVals[j] +
" b:" + bVals[j]);
outFile.close();
}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Can´t read the file", "Error",
JOptionPane.ERROR_MESSAGE);
file=null;
}
}

private void sort(int[] a1, int[] a2, int[] a3)
{
// sort a1 into ascending numerical order
// sort other arrays according to element order of a1
int index = -1;
for(int j = 0; j < a1.length; j++)
{
int min = a1[j];
for(int k = j+1; k < a1.length; k++)
{
if(a1[k] < min)
{
min = a1[k];
index = k;
}
}
if(min < a1[j])
{
swap(a1, j, index);
swap(a2, j, index);
swap(a3, j, index);
}
}
}

private void swap(int[] array, int currIndex, int minIndex)
{
int minValue = array[minIndex];
array[minIndex] = array[currIndex];
array[currIndex] = minValue;
}

public static void main(String[] args)
{
new CP();
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  IE page Redirect
•  Re: Javascript problem with document.write and accented characters
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005