Need help with simple exercise!

A

Aries

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...
 
M

Monga Monga

Aries said:
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...

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();
}
}
 
J

Jussi Piitulainen

Aries said:
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...

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?
 
A

Aries

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!
 
A

Aries

Thanks Crish, actually this is my first days of programming...A very
useful link,thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top