-2

The static method main, which receives an array of strings. The array should have two elements: the path where the files are located (at index 0), and the name of the files to process (at index 1). For example, if the name was “Walmart” then the program should use “Walmart.cmd” (from which it will read commands) and “Walmart.pro” (from which it will read/write products).

I don't want anyone to write the code for me because this is something I need to learn. However I've been reading this through and the wording is confusing. If someone could help me understand what it wants from me through pseudo-code or an algorithm it would be greatly appreciated.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2888784
  • 13
  • 1
  • 4
  • _"The static method main, which receives an array of strings."_ refers to `args`, the normal parameter of main. Your program will be run with command line arguments that are the path and file name to read/write. – Radiodef Oct 28 '13 at 22:52
  • Which part is confusing to you? – rob Oct 28 '13 at 22:52
  • Where I'm confused is how to initialize arg[0] and arg[1] and exactly what they are being initialized to. – user2888784 Oct 28 '13 at 22:57

3 Answers3

0

So lets explain to you

  • Create a class Inventory : if you don't know how to create a class google it just as is
  • The static method main: Every executable class in java (at least from the console) has the main method you should google java main method and propably in the same place you find it you will see the default arguments that it receives
  • When you learn about the default arguments of method main you will undertand about the 'args' that has to be on it
  • You will have t study the class String google it "java String class"
  • You will have to study the class File google it "java File class"

    At the end everything else would be just logic and I beleave you have learned some at this point.

  • Jorge Campos
    • 22,647
    • 7
    • 56
    • 87
    0
    public class Inventory { // class inventory
    
    public static void main(String[] args) // main method
    {
    
    if(args.length==2){ // check if args contains two elements
    String filePath = args[0];
    String fileName = args[1];
    
    filePath+= System.getProperty("file.separator")+fileName; 
    
    File fileCMD = new File(filePath+".cmd");
    //fileCMD.createNewFile(); 
    File filePRO =new File(filePath+".pro");
    //filePRO.createNewFile();
    
    
    }
    
    else {
    
    //write the code to print the message Usage: java Inventory Incorrect number of parameters for a while and exit the program.
    }
    
    }
    

    This is what I've understood. Basically you have to write a program to create two files, one called fileName.cmd and the other fileName.pro. You have to construct the path of the files using the arguments (input parameters of the main method) and system's file separator. If the arguments don't have two elements you have to print the 'invalid' message. That's it.

    Where I'm confused is how to initialize arg[0] and arg[1] and exactly what they are being initialized to.

    You have to use command line to pass the arguments and launch the program , something like the following code in cmd or terminal:

    java inventory thePath theFileName
    

    That's how it get initialized.

    0

    Where I'm confused is how to initialize arg[0] and arg[1] and exactly what they are being initialized to.

    The main method's String array input argument consists of whatever String arguments you pass to the program's main method when you run the program. For example, here is a simple program that loops over args and prints a nice message with each argument's index and value on a separate line:

    package com.example;
    
    public class MainExample {
        public static void main(String[] args) {
            for (int i = 0; i < args.length; i++) {
                System.out.printf("args[%d]=%s\n", i, args[i]);
            }
        }
    }
    

    Once you've compiled the program, you can run it on the command-line and pass it some arguments:

    java -cp . com.example.MainExample eh? be sea 1 2 3 "multiple words"
    

    Output:

    args[0]=eh?
    args[1]=be
    args[2]=sea
    args[3]=1
    args[4]=2
    args[5]=3
    args[6]=multiple words
    
    rob
    • 6,147
    • 2
    • 37
    • 56