每次运行我的程序时,它都会直接进入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();
}
}
发布于 2013-04-20 13:51:36
以以下方式运行该程序:
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
是称为Command line arguments的String
数组。因此,如果您以java DataGenerator someX someY
用户身份从命令行运行程序,那么args
将包含[someX,someY]
,args.length
将为2。
发布于 2013-04-20 13:51:52
如果您正在输入showUsage
方法,这意味着您没有提供恰好一个参数。
尝试打印一份参数副本,以查看提供的内容:
for (String s : args) {
System.out.println(s);
}
在命令行上,您需要运行java DataGenerator foo
(其中foo
是您的参数)。如果您使用的是IDE,请查看帮助以了解如何指定运行时参数。
发布于 2013-04-20 13:52:20
我将继续并猜测这是因为,当您运行程序时,没有传递任何命令行参数。String[] args
由您在运行时传递给程序的命令行参数填充。
如果没有任何参数,则if
语句的计算结果为true
,运行showUsage()
,然后运行System.exit(0)
,退出程序。
https://stackoverflow.com/questions/16117129
复制相似问题