Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda