java - How do I get out of the showUsage() method in the main method? -


every time run program, goes straight showusage method in if statement. why that? how can continue program without getting stuck in showusage method? want able run program can input data txt file unfortunately, i'm getting stuck in showusage() method.

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 generate trans.txt representing transcations data");     system.out.println(" master generate oldmast.txt representing account recorddata");           }      /**      * in loop, prompts user enter transaction record 1 @ time.       * figure out way enable user exit loop      * write out transaction records in 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 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 account number?");             int accountnum = keyboard.nextint();             system.out.println("what transaction amount?");             double transaction = keyboard.nextdouble();             outputstream.println(accountnum + "                        " + transaction);              system.out.println("do want enter more information?");             string answer;             answer = keyboard.next();             if(answer.equalsignorecase("no"))             {                 keepgoing = false;             }           }         outputstream.close();            }       /**      * in loop, prompts user enter customer account record 1 @ time.       * figure out way enable user exit loop      * write out account records in 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 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 account number?");             int accountnum = keyboard.nextint();             system.out.println("what name?");             string name = keyboard.nextline();             system.out.println("what name?");             int balance = keyboard.nextint();             outputstream.println(accountnum +      "                   " +name+"          " +        balance);              system.out.println("do want enter more information?");             string answer;             answer = keyboard.nextline();             if(answer.equalsignorecase("no"))             {                 keepgoing = false;             }           }         outputstream.close(); }   } 

run program as:

java datagenerator someargument 

coming code:

public static void main(string[] args)  {    // here checking length of args array not equal 1    // if not equal 1 then, enter if block    // avoid if block, need run program 1    // command line argument    if ( args.length != 1)    {        showusage();        system.exit(0);    }  ...............  } 

args array of string called command line arguments. so, if run program command line java datagenerator somex somey, args contain [somex,somey] , args.length 2.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -