如何退出main方法中的showUsage()方法?

时间:2013-04-20 05:48:15

标签: java

每次运行程序时,它都直接进入if语句中的showUsage方法。这是为什么?如何继续使用该程序而不会陷入showUsage方法?我希望能够运行这个程序,所以我可以将数据输入到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();
}


}

3 个答案:

答案 0 :(得分:1)

将程序运行为:

java DataGenerator someArgument

回到你的代码:

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);
   }
 ...............

}

argsString的数组,称为Command line arguments。因此,如果您从命令行以java DataGenerator someX someY运行程序,则args将包含[someX,someY]args.length将为2.

答案 1 :(得分:1)

如果您输入showUsage方法,则表示您尚未提供完全一个参数。

尝试打印参数的副本以查看提供的内容:

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

在命令行中,您需要运行java DataGenerator foo(其中foo是您的参数)。如果您使用的是IDE,请查看帮助以了解如何指定运行时参数。

答案 2 :(得分:1)

我要继续猜测是因为你在运行程序时没有传递任何命令行参数。 String[] args填充了您在运行时传递给程序的命令行参数。

如果没有任何参数,if语句将被评估为trueshowUsage()将运行,然后System.exit(0)将退出该程序。