Re: Need help with simple exercise!
Aries wrote:
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();
}
}