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