patternjavaModerate
Print a command line argument 100 times
Viewed 0 times
argumentline100printtimescommand
Problem
I am learning Java and this is my solution for a program that will read in a name from the command line and write it out 100 times furthermore words are never split up assuming that the console is 80 characters wide.
Could someone please help me improve its quality?
Could someone please help me improve its quality?
/**
* Program to print command line argument 100 times
* Words are never split up. Assuming that the console is 80 characters wide.
*
**/
class Hundred {
private static final int LINE_WIDTH = 80;
public static void main(String[] args) {
if(args.length == 0) {
System.out.println("Argument missing");
} else {
String name = args[0];
int wordsOnOneLine = LINE_WIDTH / (name.length() + 1);
int i = 0;
while(i < 100) {
for(int j = 0; j < wordsOnOneLine && i < 100; j++) {
i++;
System.out.print(name);
System.out.print(" ");
}
System.out.println();
}
}
}
}Solution
General Remarks
Well done. While there are some points of improvement, you created a functional program, with proper naming of arguments, and even some basic error handling. Asking for feedback is the best way to learn, so here you get some :). Keep up the good work.
Repeating/LineWidth logic
The repeating of the value 100 is suspicious, there probably is a better solution.
If you see this, think more and try to approach the problem differently.
The main idea (that vp_arth already posted) is to keep track of the available space in the line, no need to calculate the
Also, you can count-down the number of words you still have to output.
You will end up with two simple counters
Cleanness of output
While not really enforced, you should not add a space at the end of the line if not needed. Trailing spaces are a general pain in the ass, because you can't see them and might give adverse effects.
Validating arguments
And you need to take care your user does not apply an argument that will break your program (what if the user enters an 1000 character String?), so you need to check for that too.
Java conventions
You already follow most of the java conventions (casing, bracing, etc). But you are missing a
Exit codes
While not THAT interesting, it is good practice to let you program end with an error code (integer greater than zero) if it could not run succesfully.
Proposed solution
Well done. While there are some points of improvement, you created a functional program, with proper naming of arguments, and even some basic error handling. Asking for feedback is the best way to learn, so here you get some :). Keep up the good work.
Repeating/LineWidth logic
The repeating of the value 100 is suspicious, there probably is a better solution.
while(i < 100) {
for(int j = 0; j < wordsOnOneLine && i < 100; j++) {If you see this, think more and try to approach the problem differently.
The main idea (that vp_arth already posted) is to keep track of the available space in the line, no need to calculate the
wordsOnOneLineAlso, you can count-down the number of words you still have to output.
You will end up with two simple counters
repetitions and availableSpace. See below for more detail.Cleanness of output
While not really enforced, you should not add a space at the end of the line if not needed. Trailing spaces are a general pain in the ass, because you can't see them and might give adverse effects.
Validating arguments
And you need to take care your user does not apply an argument that will break your program (what if the user enters an 1000 character String?), so you need to check for that too.
Java conventions
You already follow most of the java conventions (casing, bracing, etc). But you are missing a
package for your class. While not needed, it is good practice.Exit codes
While not THAT interesting, it is good practice to let you program end with an error code (integer greater than zero) if it could not run succesfully.
Proposed solution
package hundred;
/**
* Program to print command line argument 100 times Words are never split up. Assuming that the console is 80 characters
* wide.
**/
public class Hundred
{
private static final int LINE_WIDTH = 80;
public static void main( String[] args )
{
if ( args.length == 0 )
{
System.err.println( "Argument missing" );
System.exit( 1 ); //error status
}
else
{
String word = args[0];
int availableWidth = LINE_WIDTH;
if ( word.length() > availableWidth )
{
System.err.println( "Argument too long for the line width" );
System.exit( 1 ); //error status
}
int repetitions = 100;
while ( repetitions > 0 )
{
if ( availableWidth word.length() + 1 )
{
System.out.print( " " );
availableWidth--;
}
repetitions--;
}
}
}
}Code Snippets
while(i < 100) {
for(int j = 0; j < wordsOnOneLine && i < 100; j++) {package hundred;
/**
* Program to print command line argument 100 times Words are never split up. Assuming that the console is 80 characters
* wide.
**/
public class Hundred
{
private static final int LINE_WIDTH = 80;
public static void main( String[] args )
{
if ( args.length == 0 )
{
System.err.println( "Argument missing" );
System.exit( 1 ); //error status
}
else
{
String word = args[0];
int availableWidth = LINE_WIDTH;
if ( word.length() > availableWidth )
{
System.err.println( "Argument too long for the line width" );
System.exit( 1 ); //error status
}
int repetitions = 100;
while ( repetitions > 0 )
{
if ( availableWidth < word.length() )
{
System.out.println();
availableWidth = LINE_WIDTH;
}
System.out.print( word );
availableWidth -= word.length();
//We might need a space, but only if the word + space still fits
if ( availableWidth > word.length() + 1 )
{
System.out.print( " " );
availableWidth--;
}
repetitions--;
}
}
}
}Context
StackExchange Code Review Q#154664, answer score: 13
Revisions (0)
No revisions yet.