2

Problem: I don't know how to appropriately call the method like the assignment instructs me to.

Objective: Within the main method, call the readFile method with the first command line argument (args[0]), then call the writeFile method with the second command line argument (args[1]).

What I've tried:

public static void main(String[] args)
{
    if(args.length == 0)
    {
        inputFile = FileIO(args[0]);
    }
    if(args.length == 1)
    {
        outputFile = FileIO(args[1]);
    }
}

Code to work with:

public class FileIO {

    public void readFile(String inputFile)
    {

    }

    public void writeFile(String outputFile) 
    {

    }

    public static void main(String[] args)
    {

    }
}

Error:

Multiple markers at this line
-inputFile cannot be resolved to a variable
-The method FileIO(String) is undefined for the type FileIO

FoxH
  • 45
  • 8

1 Answers1

1

I don't want to do the assignment for you but I can give you hints that should let you get farther.

First related to the errors you're getting, you must declare the types of variables in Java so instead of just saying

inputFile = FileIO(args[0])

you must declare the type of that variable. I'm not sure what FileIO() returns but if it's a File object you would need to declare

File inputFile = FileIO(args[0])

Without that the compiler has no idea what inputFile is and throws that error.

Next it's telling you that FileIO(String) is not a function. FileIO is just a class but you forgot to specify which function in the class you want to use. I expect you meant to use FileIO.readfile(args[0]) for example. It's not clear if your first main is an implementation of the main in FileIO or a separate file that just uses FileIO so I'd need to see more to go into more detail. Note that you must have an instance of a class (created using new FileIO() if you intend to call non-static methods in it.

Lastly, you need to understand the concept of zero-based arrays. An array with one element would have array.length tell you 1 but you would find the value at index array[0]. For example:

String[] args = ["a","b"];
args.length;  // This is 2
args[0];      // This is "a"
args[1];      // This is "b"

So the last element is always at index array.length-1.

When you say if(args.length == 0) you're really asking if the array is empty.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
  • FileIO is the class defined in the second block of code in the post. They need help understanding methods as a whole, they don't understand how to call readFile() that is defined within FileIO class – Tyler Nov 06 '19 at 18:11
  • yep - I noticed that just as I posted my first draft - edited now. – Always Learning Nov 06 '19 at 18:15