0

I am reading text from one file and appending to another file using parallel string arrays, but I keep getting the error Market.java:84: error: no suitable method found for write(String,String,String,String,String,String,String) and I can't find a way to fix it. My Program:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Market {

    private FileWriter output;

    String[] market = new String[100];
    String[] name = new String[100];
    String[] street = new String[100];
    String[] city = new String[100];
    String[] state = new String[100];
    String[] country = new String[100];

    public void openFile() {
        try {
            output = new FileWriter("report.txt", true);
        } catch (SecurityException securityException) {
            System.err.println("You do not have write access to this file.");
            System.exit(1);
        } catch (FileNotFoundException fileNotFoundException) {
            System.err.println("Error opening or creating file.");
            System.exit(1);
        }
    }

    public void ReadMarket() {

        try {
            BufferedReader readbuffer = new BufferedReader(new FileReader("markets.txt"));
            String strRead;

            while ((strRead = readbuffer.readLine()) != null) {

                int i = 0;

                String splitarray[] = strRead.split("\t");
                String firstentry = splitarray[0];
                String secondentry = splitarray[1];
                String thirdentry = splitarray[2];
                String fourthentry = splitarray[3];
                String fithentry = splitarray[4];
                String sixthentry = splitarray[5];

                market[i] = firstentry;
                name[i] = secondentry;
                street[i] = thirdentry;
                city[i] = fourthentry;
                state[i] = fithentry;
                country[i] = sixthentry;

                Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]);
                i++;
            }
        }

        catch (IOException e) {
            System.out.println("Not Working");
        }
    }

    public void closeFile() {
        output.close();
    }
}

4 Answers 4

1

I believe that's because the method:

     Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]); 

Doesn't exist, please link to a javadoc if you know of such a method existing!

Sign up to request clarification or add additional context in comments.

Comments

1

Change the following line:

Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]);

to:

output.write(String.format("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]));

Reason for the error:

  1. write() in Writer class is not static and you are trying to use it as static.
  2. write() in Writer class does not have a method to take variable number of arguments.
  3. You have created instance of FileWriter (output) and you should use it to write the data.

1 Comment

Good solution, thank you for providing it on top of the explanation of the error!
0

That's because there is no such method in the FileWriter class. To write the data in the format you require, you can do something like this

String toWriteString = String.format("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]); // Use String format to get the String in the format you required and then write that to the file
output.write(toWriteString); // I believe output is the FileWriter object from what I see in the code

Comments

0

Writer.write does not take the amount of parameters you are trying to pass. One of the write method takes a String. So you may want to format the contents in a string and then pass on to write method.

Try this:

Writer.write(String.format("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i])); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.