Java: Repeat a command

I came across a site to learn java and they give you practice problems to do. I was wondering if anyone can help me with this since I am totally new to Java. Here is the first problem:
Write a program that will read in a name from the command line and write it out 100 times.
Thank you for any help.

I'm confused. You found a site where you can learn to write java code. But, instead of using that site to learn how to write java code, you want us to write java code for you instead.

Instead of giving us questions to see if we know java, why don't you try writing the code yourself. If you run into problems, show us what you have done, show us the output you're trying to get, show us the output you are getting, and show us any diagnostics your code is producing. Maybe we can help you learn from your mistakes instead of doing all of the work for you (so you don't learn anything at all).

Sorry, I usually like to look at complete code first to learn and then try.
I wrote this:

public class prob1
{

    public static void main( String[] args )
    {
	 Scanner user_input = new Scanner(System.in);
	 
	 String name;
	 System.out.print("Enter your first name: ");
	 name = user_input.next();
	 
	 int i;
	 int n = 100;
	 for (i=0; i<n; i++)
		System.out.print(name);

    }

}

I am not sure if it works or not because I do not know how to run it on the command line. I tried java prob1 and received this error: Error: Could not find or load main class prob1
Then I tried javac prob1 (I thought this would compile it) but I received this:
error: Class names, 'prob1', are only accepted if annotation processing is explicitly requested
I never tried running a java code on unix. I wrote the code using vim.

Rename your sourcefile prob1 to prob1.java and retry with javac prob1.java

Source: Lesson: Common Problems (and Their Solutions) (The Java� Tutorials > Getting Started)

Thank you! But now it saying my scanner function is not working. Is there another way to take in user input?

Without seeing the actual error message, I'd say that can be fixed by adding following line on the top of your source code file:

import java.util.Scanner;

Yes. You can experiment with BufferedReader (Java BufferedReader & InputStreamReader example - get keyboard/console input - voidException) or DataInputStream (java DataInputStream class | Magic2Php).

---------- Post updated at 04:39 PM ---------- Previous update was at 04:34 PM ----------

Also consider using a full-size IDE for Java programming, e.g. Eclipse, NetBeans etc.

Oh thank you! It works!
The site I am looking at is telling me to do it a different way.
They want you to be able to type:

         java Hundred Gertrude

in the command line and see the word Gertrude 100 times. How would you do it that way?

This means the program's name is Hundred (source file name Hundred.java) and Gertrude is the argument.

The first argument will be stored in args[0], thus you can simply put following in the for loop (you know already how to build a for loop which will repeat a command n times):

        System.out.println(args[0]);

You could also do it like this:

        String ArgumentFromCommandLine = args[0];

        for loop ...
        System.out.println(ArgumentFromCommandLine);

Hope this helps.

1 Like

Thank you again! I did it like this:

public class prob1
{

    public static void main( String[] args )
    {
    String ArgumentFromCommandLine = args[0];

    int i;
    int n = 100;
    for (i=0; i<n; i++)
      System.out.print(ArgumentFromCommandLine);

    }

}

Last question about this. The way you showed me made the output show up on different lines so I did it the other way so it will just show up on the same line. However, there are no spaces between the words. How can I fix that? I tried this:

int i;
    int n = 100;
    for (i=0; i<n; i++)
      System.out.print(ArgumentFromCommandLine);
      System.out.print(" ");

But it did not work.

You seem to want something more like:

    int i;
    int n = 100;
    for (i=0; i<n; i++) {
        System.out.print(ArgumentFromCommandLine);
        System.out.print(" ");
    }

What you have written could more clearly be written as:

    int i;
    int n = 100;
    for (i=0; i<n; i++) {
        System.out.print(ArgumentFromCommandLine);
    }
    System.out.print(" ");
1 Like

Looks like only one command is executed within the for loop if you leave out the braces { and } in the for loop.

Either you use Don's first approach with braces, or you try this:

      System.out.print(ArgumentFromCommandLine + " "); // single quotes should work too

Note that all approaches mentioned here will produce a superfluous whitespace at the end of the output. One possible solution:

    int i;
    int n = 99;
    for (i=0; i<n; i++)
      System.out.print(ArgumentFromCommandLine + " "); // executed 99 times
      System.out.print(ArgumentFromCommandLine); // executed 1 time, System.out.println recommended here
1 Like