im struggling with the below question.
Question: Input contains the number of blocks n (1 ≤ n ≤ 20) and weights of the blocks w1, …, wn (integers, 1 ≤ wi ≤ 100000) delimited by white spaces. the input should be taken from the user.
- how should i write the input with spaces? (lets say my input for no. of blocks is n =5 and the weights of the blocks be : 2 3 55 6 33 - because n=5)
- how should i read the input which are given with white spaces and store it in some list or array ? using what commands.
please find my code:
public static void main(String[] args) {
int b1 = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter no. of blocks: ");
b1 = in.nextInt();
if (b1<=20) {
in.nextLine();
int[] arr = new int[b1];
for (int i=0; i<b1; i++) {
System.out.println("Enter a weights of ths blocks: ");
if (arr[i]<=100000) {
arr[i] = in.nextInt();
}
}
}
i dont think this is the right way coz the input should be delimited with spaces..
i was thinking about the ways to proceed but couldnt come up with any solution. can u please help me our on this guys. thanks.