Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Command Line Arguments

Posted on 2003-02-23
1
Medium Priority
?
199 Views
Last Modified: 2010-03-31
here is my code.
What I am trying to do is check for command line arguements passed the program. Just wondered if I was going about it the right way.

I want to have the arguements
-t
-b
-s
and a no arguements method.

Also If I want to output my above Address to a text file do i need to attach a DataOutputSteam to it here or in my Main Address.java! I am new to I/O

Thanlks





import java.io.*;
import javabook.*;


class AddressMaker
{
   public static void main (String[] args)
   {

Address address = new Address( My House, A Road,
           The Town, UK, POST CODE);

   if (args.length > 0) // Check to see if a parameter is passed
    {
     for (int argNo = 0; argNo < args.length; argNo ++) // Look at parameters passed
     {
      if (args[argNo].startsWith("-t")
      {
      }
      else
        if (args[argNo].startsWith("-b")
        {
        }
        else
          if (args[argNo].startsWith("-s")
          {
          }
          else // If no argument passed
          {
         System.out.println(":"+ address.toString());
          }
   }
}
0
Comment
Question by:welsh_boy
1 Comment
 
LVL 5

Accepted Solution

by:
vemul earned 240 total points
ID: 8003074
yes, this looks fine. About writing to a file, u can put it in this class itself as it looks like your Address class is used basically to store/manipulate values with its variables

PrintWriter printWriter1;
     try
         {
              printWriter1   = new PrintWriter(new BufferedWriter(new FileWriter(outFile1)));
              printWriter1.flush();
              printWriter1.println(address);
              printWriter1.close();
         }
         catch(Exception e)
         {
              e.printStackTrace();
         }


also do note that when u write address as an object into the file, you may want to define your own toString() method in your address class to make it readable in your txt file or you can store the names, city etc by saying
              printWriter1.println(address.getName());
              printWriter1.println(address.getPostCode());

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question