Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Why Is Java Program Not Recognizing Second Command Line Argument?

Building a Random Number Generator that takes a upper boundary input, and numbers to be generated by the user and prints the results.

For whatever reason my program (which compiles), doesn't recognize the boundary argument as the second argument and exits per my checking of arguments.

Any help would be appreciated in my understanding of how to fix this and what I may be missing.

Thanks!

import java.util.Random;
import java.util.Scanner;

public class RandNumGen{
    public static void main(String[] args){

// intro
System.out.println("*****************************************");
System.out.println("*Welcome to the Random Number Generator!*");
System.out.println("*    Discussion 7: Michael Stadnicki    *");
System.out.println("*****************************************");

// ask user to input numbers to be generated
System.out.println("Please enter how many random numbers you would like generated!");
Scanner numGen = new Scanner(System.in);
    int randomNum = numGen.nextInt();
System.out.println("We will generate " + randomNum +" random numbers!");

// ask user to input out boundary for the random numbers
System.out.println("Please enter the boundary for our random numbers to be generated!");
Scanner boundGen = new Scanner(System.in);
    int randomBound = boundGen.nextInt();
System.out.println("We will use " + randomBound +" as our boundary for generation!");

// build our random number generator
Random rNum = new Random();

// check our arguments
if (args.length == 2) {
    randomNum = Integer.parseInt(args[0]);
    randomBound = Integer.parseInt(args[1]);
} else {
    System.out.println("You are required to have 2 arguments on the command line!");
    System.exit(0);
}

// print our random numbers generated based on above bounds and numbers
for (int i = 0; i <randomNum; i++) {
    int numOutput = rNum.nextInt(randomBound + 1);
    System.out.println("Your random numbers are: " + numOutput);
}

}

}

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try only asking for user imput, don't use args. And don't use two different scanners, just call numGen.nextInt(); two times.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!