简体   繁体   English

如何摆脱main方法中的showUsage()方法?

[英]How do I get out of the showUsage() method in the main method?

Every time I run my program, it goes straight into the showUsage method in the if statement. 每次运行程序时,它将直接进入if语句中的showUsage方法。 Why is that? 这是为什么? How can I continue with the program without getting stuck in the showUsage method? 如何继续执行程序而不会陷入showUsage方法中? I want to be able to run this program so I can input the data into txt file but unfortunately, I'm getting stuck in the showUsage() method. 我希望能够运行该程序,以便可以将数据输入到txt文件中,但是不幸的是,我陷入了showUsage()方法中。

public class DataGenerator 
{
    public static void main(String[] args) 
    {

        if ( args.length != 1)
       {
           showUsage();
           System.exit(0);
       }


       String target = args[0];  
       switch (target)
       {
          case "trans" :
             generateTransactionRecords();
             break; 
          case "master" :
             generateAccountRecords(); 
             break;
          default : 
              showUsage();     
       }



    }

    private static void showUsage()
    {
    System.out.println("Usage: DataGenerator trans|master");
    System.out.println(" trans to generate trans.txt representing transcations data");
    System.out.println(" master to generate oldmast.txt representing account recorddata");      
    }

    /**
     * In a loop, prompts the user to enter transaction record one at time. 
     * Figure out a way to enable user to exit the loop
     * Write out all the transaction records in a text file called trans.txt  
     * 
     */
    private static void generateTransactionRecords()
    {
        System.out.println("generateTransactionRecords() called");
        Scanner keyboard= new Scanner (System.in);
        String [] args = new String [1];
        args[0] = "trans.txt";
        //String fileName = arg[0];//"trans.txt";
        //String fileName = "";
        PrintWriter outputStream = null;

        try
        {
        outputStream = new PrintWriter(args[0]);//fileName);

        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file" + args[0]);
            System.exit(0);
        }

        outputStream.println ("Transaction file" + "        " + "Transaction");
        outputStream.println ("account number" +   "        " + "    amount");
        boolean keepGoing = true;
        while (keepGoing)
        {

            System.out.println("What is the account number?");
            int accountNum = keyboard.nextInt();
            System.out.println("What is the transaction amount?");
            double TransAction = keyboard.nextDouble();
            outputStream.println(accountNum + "                        " + TransAction);

            System.out.println("Do you want to enter more information?");
            String answer;
            answer = keyboard.next();
            if(answer.equalsIgnoreCase("no"))
            {
                keepGoing = false;
            }


        }
        outputStream.close();       
    }


    /**
     * In a loop, prompts the user to enter customer account record one at time. 
     * Figure out a way to enable user to exit the loop
     * Write out all the account records in a text file called oldmast.txt  
     * 
     */
    private static void generateAccountRecords()
    {
        System.out.println("generateAccountRecords() called");
        Scanner keyboard= new Scanner (System.in);

        String [] args = new String [1];
        args[0] = "trans.txt";
        PrintWriter outputStream = null;

        try
        {
        outputStream = new PrintWriter(args[0]);

        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file" + args[0]);
            System.exit(0);
        }

        outputStream.println ("Master file");
        outputStream.println  ("account number" +   "       Name" + "      Balance");
        boolean keepGoing = true;
        while (keepGoing)
        {
            System.out.println("What is the account number?");
            int accountNum = keyboard.nextInt();
            System.out.println("What is the name?");
            String name = keyboard.nextLine();
            System.out.println("What is the name?");
            int Balance = keyboard.nextInt();
            outputStream.println(accountNum +      "                   " +name+"          " +        Balance);

            System.out.println("Do you want to enter more information?");
            String answer;
            answer = keyboard.nextLine();
            if(answer.equalsIgnoreCase("no"))
            {
                keepGoing = false;
            }


        }
        outputStream.close();
}


}

Run the program as: 运行程序为:

java DataGenerator someArgument

Coming back to your code: 回到您的代码:

public static void main(String[] args) 
{
   // here you are checking for the length of the args array not equal to 1
   // If its not equal to one then, enter the if block
   // To avoid the if block, you need to run the program with exactly 1
   // command line argument
   if ( args.length != 1)
   {
       showUsage();
       System.exit(0);
   }
 ...............

}

args is an array of String which are called Command line arguments . argsString的数组,称为命令行参数 So, if you run your program from command line as java DataGenerator someX someY , then args will contain [someX,someY] and args.length will be 2. 因此,如果您以java DataGenerator someX someY从命令行运行程序,则args将包含[someX,someY]args.length将为2。

If you are entering the showUsage method, it means you haven't supplied exactly one argument. 如果输入的是showUsage方法,则意味着您没有完全提供一个参数。

Try printing a copy of the arguments to see what was supplied: 尝试打印参数的副本以查看提供的内容:

for (String s : args) {
  System.out.println(s);
}

On the command line, you need to run java DataGenerator foo (where foo is your argument). 在命令行上,您需要运行java DataGenerator foo (其中foo是您的参数)。 If you are using an IDE, check the help to understand how to specify runtime arguments. 如果使用的是IDE,请查看帮助以了解如何指定运行时参数。

I'm going to go ahead and guess it's because you aren't passing any command line arguments when you run the program. 我将继续猜测这是因为在运行程序时您没有传递任何命令行参数。 String[] args is filled with the command line arguments you pass to the program at runtime. String[] args填充有您在运行时传递给程序的命令行参数。

If there aren't any arguments the if statement will be evaluated to true and showUsage() will run followed by System.exit(0) which exits the program. 如果没有任何参数, if语句将被评估为true并且showUsage()将运行,然后是System.exit(0) ,它将退出程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM