-1
import java.util.*;
import java.io.*;

public class comparing{

static ArrayList <compare> events = new ArrayList<compare>();

public static void main(String[]args){

  try{
     Scanner in = new Scanner(new File("events.txt"));
     File output = new File("chines.txt");
     Scanner sc = new Scanner(output);
     PrintWriter printer = new PrintWriter(output);

     while(in.hasNext()){

        int temp = in.nextInt();
        String temptwo = in.nextLine();
        //String s = ;

        events.add(new compare(temp,temptwo));
     //System.out.println("Next word is:  " + temp);

        Collections.sort(events);
        for(int i = 0;i<events.size();i++){

           printer.write(events.get(i));
           System.out.println(events.get(i));
        }
     }
  }
  catch(Exception e){
     System.out.println("Invalid file name");
  }
}

My code above reads from a file it sorts the data then prints it out. What I would like to do is write this sorted data to another file but I keep getting the following error:

  comparing.java:27: error: no suitable method found for write(compare)
               printer.write(events.get(i));  
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
Daniel
  • 13
  • 5

3 Answers3

0

You're declaring events to be an ArrayList, meaning that it contains java.lang.Object elements, thus printer.write(java.lang.Object) is what's being searched for by the compiler.

You're adding an object of your undisclosed class compare, so even declaring ArrayList<compare> wouldn't help. Hopefully your compare class has a meaningful toString, so that you can use ArrayList<compare> events, combined with printer.write(event.toString());

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
0

See the docs.
There is no write(Object) method. You can change it to write(events.get(i).toString()) to convert it to a String first.

Alternatively, use print instead of write for more input options. See write() vs print().
It can take a object as argument and calls the String.valueOf(obj) method for you:

printer.print(events.get(i));
Community
  • 1
  • 1
RobCo
  • 6,240
  • 2
  • 19
  • 26
  • thank you very much , sorry for my messy code. If you don't mind me asking will printer.print still write to the file ? – Daniel May 01 '17 at 09:18
  • Yes, the print methods will internally call `write`. The difference is only in how it interpets the input you give it. You can get some examples of this if you search for 'printwriter write vs print' – RobCo May 01 '17 at 09:21
  • I just don't understand how it would work because printer isn't a static variable and if i use it inside my for loop I will get an error. I tried making in static and putting the for loop inside the while loop but it didn't work – Daniel May 01 '17 at 10:25
  • What is the error? It shouldn't have anything to do with it being a static reference. But don't forget to close the stream by calling `close()` on the writer when you are done. – RobCo May 01 '17 at 12:04
0

Add this code instead of your loop

           for (int i = 0; i < events.size(); i++) 
            {
                printer.write(String.valueOf(events.get(i)));
                out.write(" ");
            }
Abubakar
  • 374
  • 3
  • 16