Here's a simple code that reads strings from stdin, adds them into List<String>, and then uses toArray to convert it to String[] (if you really need to work with arrays).
import java.util.*;
public class UserInput {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Scanner stdin = new Scanner(System.in);
do {
System.out.println("Current list is " + list);
System.out.println("Add more? (y/n)");
if (stdin.next().startsWith("y")) {
System.out.println("Enter : ");
list.add(stdin.next());
} else {
break;
}
} while (true);
stdin.close();
System.out.println("List is " + list);
String[] arr = list.toArray(new String[0]);
System.out.println("Array is " + Arrays.toString(arr));
}
}
See also:
- Why is it preferred to use Lists instead of Arrays in Java?
- Fill a array with List data
Here's a simple code that reads strings from stdin, adds them into List<String>, and then uses toArray to convert it to String[] (if you really need to work with arrays).
import java.util.*;
public class UserInput {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Scanner stdin = new Scanner(System.in);
do {
System.out.println("Current list is " + list);
System.out.println("Add more? (y/n)");
if (stdin.next().startsWith("y")) {
System.out.println("Enter : ");
list.add(stdin.next());
} else {
break;
}
} while (true);
stdin.close();
System.out.println("List is " + list);
String[] arr = list.toArray(new String[0]);
System.out.println("Array is " + Arrays.toString(arr));
}
}
See also:
- Why is it preferred to use Lists instead of Arrays in Java?
- Fill a array with List data
package userinput;
import java.util.Scanner;
public class USERINPUT {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//allow user input;
System.out.println("How many numbers do you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " + num + " numbers now.");
for (int i = 0 ; i < array.length; i++ ) {
array[i] = input.nextInt();
}
//you notice that now the elements have been stored in the array .. array[]
System.out.println("These are the numbers you have entered.");
printArray(array);
input.close();
}
//this method prints the elements in an array......
//if this case is true, then that's enough to prove to you that the user input has //been stored in an array!!!!!!!
public static void printArray(int arr[]){
int n = arr.length;
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
You are already splitting the input into an array, but not retaining it. Try this:
Scanner in = new Scanner(System.in);
String inputR = in.nextLine();
int lengthArgs = 5;
String[] values inputR.split(" ");
if(values.length == lengthArgs) {
int[] numbers = new int[values.length];
for(int i = 0;i < lengthArgs;i++) {
// Note that this is assuming valid input
// If you want to check then add a try/catch
numbers[i] = Integer.parseInt(values [i]);
}
}else {
System.out.println("Wrong");
Another idea, not so simple as array, but more elegant is with using Collection of Integers, like this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String inputR = in.nextLine();
String inputs[] = inputR.split(" ");
if(inputs.length != 5) {
System.out.println("Invalid number of input");
}
List<Integer> integers = new ArrayList<>();
for (String s : inputs) {
Integer number = Integer.parseInt(s);
integers.add(number);
}
System.out.println("Got following numbers in my input: " + integers);
}
For future reference, here's a way to do it with the Scanner class:
import java.util.Arrays;
import java.util.Scanner;
public class NumberReading {
public static int [] readNumsFromCommandLine() {
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine(); // throw away the newline.
int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
for (int i = 0; i < count; i++) {
if (numScanner.hasNextInt()) {
numbers[i] = numScanner.nextInt();
} else {
System.out.println("You didn't provide enough numbers");
break;
}
}
return numbers;
}
public static void main(String[] args) {
int[] numbers = readNumsFromCommandLine();
System.out.println(Arrays.toString(numbers));
}
}
You said you could use the following code in C++:
for(i=0;i<6;i++)
cin>>a[i];
If this is what you used in C++, why not just use this?
for(i=0;i<6;i++)
a[i] = input.nextInt();
Assuming that the scanner declaration was:
Scanner input = new Scanner(System.in);
From what I've seen, people are trying to write the entire program for you. But seeing as you stated you could solve this problem using the C++ code, I just converted the code to Java for you.
If you want the full code, here it is:
import java.util.Scanner;
public class arrayFun {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Create a new array. The user enters the size
int[] array = new int[input.nextInt()];
// Get the value of each element in the array
for(int i = 0; i < array.length; i++)
array[i] = input.nextInt();
/* Note that the user can type the following:
* 6
* 34 12 7 4 22 15
* This will set the array to size 6
* array[0] = 34;
* array[1] = 12;
* etc.
*/
}
}
I realize that this response is years late, but I was searching for a different question when I ran across this. The answers here were very confusing to me until I realized this wasn't what I was looking for. Seeing as this still shows up on google, I decided to post this.