Command Line Arguments

Posted on 2003-02-23
Medium Priority
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
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


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")
        if (args[argNo].startsWith("-b")
          if (args[argNo].startsWith("-s")
          else // If no argument passed
         System.out.println(":"+ address.toString());
Question by:welsh_boy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

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;
              printWriter1   = new PrintWriter(new BufferedWriter(new FileWriter(outFile1)));
         catch(Exception e)

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


Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a vā€¦
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 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