Modify the command-line argument example Hello.java, to handle a few command-line arguments. Ask the user to enter a name followed by a number. The program should print out the name N times, while N is the input number. For example, if the command-line input is Java Hello Jim R. Joseph 5 Then the output should

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter2: Using Data
Section: Chapter Questions
Problem 15PE
icon
Related questions
Question

Modify the command-line argument example Hello.java, to handle a few command-line arguments. Ask the user to enter a name followed by a number. The program should print out the name N times, while N is the input number.

For example, if the command-line input is

Java Hello Jim R. Joseph 5

Then the output should be:

 

Jim R. Joseph

Jim R. Joseph

Jim R. Joseph

Jim R. Joseph

Jim R. Joseph

 

Now what I got was this which is incorrect

import java.util.Scanner;
public class Main{
 public static void main(String[] args) {
  Scanner input = new Scanner (System.in);
  System.out.print("Enter the name: ");
        String name = input.nextLine();
        System.out.print("Enter the value of n: ");
        int num = input.nextInt();
        int j = 0;
        while(j < num){
            System.out.println(name);
            j++;
        }
 }
}

 

But the correct way to do it is to use the command-line arguments to send in the name and number, instead of the Scanner. So you can retrieve the name from args[0] and number from args[3].

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT