0

每次我运行我的程序时,它都会直接进入 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();
}


}
4

3 回答 3

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

}

args是一个String称为命令行参数的数组。所以,如果你从命令行运行你的程序 as java DataGenerator someX someY,那么args将包含[someX,someY]并且args.length将是 2。

于 2013-04-20T05:51:35.700 回答
1

如果您正在输入该showUsage方法,则意味着您没有提供确切的一个参数。

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

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

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

于 2013-04-20T05:51:52.290 回答
1

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

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

于 2013-04-20T05:52:19.563 回答