Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda