I have two problems I cannot seem to solve. First on is I need to beable to write to a file and then read that data back. I am able to write to the file but cannot read the information back. The Second problem is that I need to print my sets in reverse order. I thought I knew how to do it but nothing seems to happen when I run the command. I have attached my code for your review.import java.util.*; import java.io.*;
public class TestSort implements Serializable { static int n; static int nn;
public TestSort() { TestSortDriver.promptUsers(); set1(); } public void set1() { Integer[] set1 = new Integer[n]; TreeSet tree1 = new TreeSet(); /** generates random numbers for set # 1*/ for (int i = 0; i < set1.length; i++) { set1[i] = new Integer( (int) (Math.random() * nn)); /** adding set1 to tree1*/ tree1.add(set1[i]); } Collections.reverseOrder(); System.out.println("\nset 1: \t"); /** Should print in reverse !!! Doesn't work*/ Collections.reverseOrder(); TestSortDriver.printSet(tree1); // // } // public void printSet2(){ // This works prints out set 2 Integer[] set2 = new Integer[n]; TreeSet tree2 = new TreeSet(); TreeSet tree3 = new TreeSet(); TreeSet tree4 = new TreeSet(); /** generates random numbers for set # 2 */ for (int i = 0; i < set2.length; i++) { set2[i] = new Integer( (int) (Math.random() * nn)); // adds set2 into tree2 tree2.add(set2[i]); //tree1.add(set1[i]); tree3.addAll(tree1); tree4.addAll(tree2); } Collections.reverseOrder(); System.out.println("\nSet 2: "); TestSortDriver.printSet(tree2); Collections.reverseOrder(); tree1.addAll(tree2); Collections.reverseOrder(); System.out.println("\nUnion: " + tree1); Set intersection = new TreeSet(tree2); Collections.reverseOrder(); intersection.retainAll(tree3); Collections.reverseOrder(); //System.out.println("tree2" + tree2); System.out.println("Intersection: " + intersection); try { // create file and output object streams FileOutputStream outputItemFile = new FileOutputStream("SortedSet.txt"); ObjectOutputStream out = new ObjectOutputStream(outputItemFile); out.writeObject("\nUnion: " + tree1); out.writeObject("\nIntersection: " + intersection); out.writeObject("\nSet 1: " + tree3); out.writeObject("\nSet 2: " + tree2); out.close(); } catch (Exception error) { System.err.println("Error opening file 1"); } try { System.out.println("This is the begining of the input"); FileInputStream inputItemFile = new FileInputStream("SortedSet.txt"); System.out.println("This is after fileinputstream"); ObjectInputStream in = new ObjectInputStream(inputItemFile); System.out.println("This is after the objectinputstream"); tree1 = (TreeSet) in.readObject(); intersection = (TreeSet) in.readObject(); tree3 = (TreeSet) in.readObject(); tree2 = (TreeSet) in.readObject();
System.out.println("This is ofter the set object"); in.close(); System.out.println("This is before the for loop in the input"); for (int t = 0; t < t++; ) { System.out.println("This is after the for loop"); System.out.println("Union: " + tree1); System.out.println("Intersection: " + intersection); System.out.println("Set1: " + tree3); System.out.println("Set2: " + tree2); } System.out.println("This is the end of the input"); } catch (Exception error) { System.err.println("Error opening file 2"); // } } //} } // This code below is my main method.
public class TestSortDriver { public TestSortDriver() { } public static void main(String[] args) { {TestSort test = new TestSort();} } /** Iterator for set 1*/ public static void printSet( SortedSet set1 ) { Iterator iterator = set1.iterator(); System.out.print("{"); while ( iterator.hasNext() ) System.out.print((Integer) iterator.next() + "," ); System.out.println("}"); } /** Iterator for set 2*/ public static void printSet2( SortedSet set2 ) { Iterator iterator = set2.iterator(); System.out.print("{"); while ( iterator.hasNext() ) System.out.print( iterator.next() + "," ); System.out.println("}"); } /** prompts the users*/ public static void promptUsers() { /** Asks the user to enter the total of number in a set (array)*/ TestSort.n = promptForInt("\nEnter the array: \t"); /**Asks the user to enter the number range they want in a set*/ TestSort.nn = promptForInt("\nEnter the number range for the sets:\t"); } public static int promptForInt(String prompt) { System.out.print(prompt); System.out.flush(); String s = ""; BufferedReader ds = new BufferedReader(new InputStreamReader(System.in)); try{s = ds.readLine(); } catch (IOException e){System.out.println(e); } return Integer.parseInt(s); } }
Linda Burritt
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Hi Linda, Your first problem seems easy to solve if you look carefully at your code. Instead of writing your objects with comments:
Just write the objects:
Your second question is (if I understood well): how to use a TreeSet object with a reverse ordering? To enable reverse sorting in a TreeSet, build your object with the following construction code:
Now set2 will use inverse natural ordering when adding items. If you prefer to reverse a existing TreeSet:
Cheers
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.