+ 7
How to pass numbers entered by user in the variable length arguments in java
I want to pass the numbers directly into varargs in a function by user input directly ie without using any array . Is there any way .
3 Respuestas
+ 5
Thanks for the answer
+ 3
Hello, let me see if I understood your question correctly: Do you want to ask the user directly for input and then store that in a variable? If so, this is one solution:
//Import the Scanner class.
import java.util.Scanner;
//Get a new Scanner object.
Scanner input = new Scanner(System.in);
//Create your variable, prompt the user for input and store it.
String test = input.next();
//If it's an integer:
int test = input.nextInt();
Note: you can ask for multiple user input by (for example) creating a 'for' loop and asking the user for input inside said loop.
Hope that helps!
+ 3
But i actually want to know about passing values directly to variable length arguments through user input...