首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在main方法中退出showUsage()方法?

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

Stack Overflow用户
提问于 2013-04-20 13:48:15
回答 3查看 166关注 0票数 0

每次运行我的程序时,它都会直接进入if语句中的showUsage方法。为什么会这样呢?我怎样才能在不陷入showUsage方法的情况下继续程序呢?我希望能够运行这个程序,这样我就可以将数据输入到txt文件中,但不幸的是,我被showUsage()方法卡住了。

代码语言:javascript
运行
复制
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();
}


}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-20 13:51:36

以以下方式运行该程序:

代码语言:javascript
运行
复制
java DataGenerator someArgument

回到你的代码:

代码语言:javascript
运行
复制
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是称为Command line argumentsString数组。因此,如果您以java DataGenerator someX someY用户身份从命令行运行程序,那么args将包含[someX,someY]args.length将为2。

票数 1
EN

Stack Overflow用户

发布于 2013-04-20 13:51:52

如果您正在输入showUsage方法,这意味着您没有提供恰好一个参数。

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

代码语言:javascript
运行
复制
for (String s : args) {
  System.out.println(s);
}

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

票数 1
EN

Stack Overflow用户

发布于 2013-04-20 13:52:20

我将继续并猜测这是因为,当您运行程序时,没有传递任何命令行参数。String[] args由您在运行时传递给程序的命令行参数填充。

如果没有任何参数,则if语句的计算结果为true,运行showUsage(),然后运行System.exit(0),退出程序。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16117129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档