Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Need help with simple exercise!

Thread view: 
Aries - 03 May 2006 02:50 GMT
Hello, i am new to programming.Here is a simple exercise:
Write a simple class that iterates through all the arguments passed in
on the command line and prints them out to the screen.
If no arguments are specified print a message explaining to the user
that they need to supply some arguments.

Could u help me pls...
Roedy Green - 03 May 2006 04:16 GMT
> Could u help me pls...

see http://mindprod.com/jgloss/newsgroups.html
for hints on how to maximise your odds of a reply.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Bart Cremers - 03 May 2006 07:23 GMT
Read following you page and you might be capable of doing the assigment
yourself and learn something:

http://java.sun.com/docs/books/tutorial/essential/attributes/cmdLineArgs.html

Bart
Monga Monga - 03 May 2006 10:38 GMT
> Hello, i am new to programming.Here is a simple exercise:
> Write a simple class that iterates through all the arguments passed in
[quoted text clipped - 3 lines]
>
>  Could u help me pls...

OK mfana, I will show you how to do this in java, but though keep in
mind that you can do more to improve the code and that I leave for you,
Hola!!

import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class SimpleExercise
{
   // Throws an exception to indicate that a problem occured due to
input operations
    public static void main(String[] args)throws IOException
    {

       BufferedReader reader = new BufferedReader(new
InputStreamReade(System.in));
       System.out.println("Please enter a line of text");

       /*Read the line of text from the consol window, the method
returnsnull    if no line was entered*/
       String input = reader.readLine();

       boolean done = false;

       while(!done)
       {
          if(input == null)
          {
              done = true;
              System.out.println("Enter arguments on the console
window");
          }
          else
          {
              //This a class used to break up a sentence into words
              StringTokenizer token = new StringTokenizer(input);
              while(token.hasMoreTokens())
              {
                  String word = token.nextToken();
                  System.out.println(word);
              }
              input = reader.readLine();
          }  
      }
      reader.close();
   }
}
Aries - 03 May 2006 11:41 GMT
Thanks Monga Monga, I see you are an experienced programmer.Thanks a
lot, I really appreciate it.I have some questions.
1.Couldn't i import just java.io* ?Why do I need to import all this
stuff like java.util.stringtokenizer?
2. BufferedReader reader = new BufferedReader(new
InputStreamReade(System.in)); Why is that needed for?Is it a
class/method whis is essential for getting input from the user as far
as i understood?
3.Please comment this line of code
Boolean done = false
Are you creating variable named done of type boolean?What is 'done'?
Shar your knowledge!
chris brat - 03 May 2006 12:32 GMT
Aries,

It doesn't seem that you know much about Java - not a flame, just an
observation.

Take a look at these tutorials on the Sun Java site - it should answer
all your questions and  make these problems easier in future.

http://java.sun.com/docs/books/tutorial/index.html

Chris
Aries - 03 May 2006 13:00 GMT
Thanks Crish, actually this is my first days of programming...A very
useful link,thanks again.
Jussi Piitulainen - 03 May 2006 11:24 GMT
> Hello, i am new to programming.Here is a simple exercise: Write
> a simple class that iterates through all the arguments passed
[quoted text clipped - 3 lines]
>
>  Could u help me pls...

Can you write a program that prints "Hello, world!" no matter
what? Write that first. The command to print "Hello, world!" is
spelled System.out.println("Hello, world!");.

Then modify the program so that it prints "Hello, world!" if
there are command line arguments, and prints another message
otherwise.

When you have done that, you know how to write a runnable program
and how to run it, and you also know where to find the command
line arguments and how many they are, and how to choose what to
do based on a condition being true or false.

Now modify the program to print out the first command line
argument, if there are command line arguments. Print a message
otherwise.

When you have done that, you know how to access individual
command line arguments. You have found out that the index of the
first argument in the array - they are in a thing that is called
an array - is 0.

Then you are ready to learn about loops. First modify the program
to print out the first command line argument as many times as
there are command line arguments: print huuh huuh huuh, each on
its own line, if the arguments are huuh, ka and ja. Use a so
called "for" loop. If it takes an hour to understand how it
works, spend the hour.

At this point it is really simple to finnish the job and let the
huuhkaja out of its cage. Typically you change a single character
only: a zero to ... what?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.