+ 2
How to get the length of a scanner input?
If my input is scanner that ask for 3 inputs. Example input 5 4 1 I want to check that I have exactly three inputs before I run the code. How I can do that. I tried to put them in a string and check the length but it didn't work.
7 ответов
+ 10
Aah AJ #Learn With Me 👨💻 my bad😅.Thank you for your suggestion 🤗
+ 9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String k=sc.nextLine();
int s=0;
String[] l=k.split(" ");
System.out.println(l.length);
}
}
Here is the code it gives how many elements we entered .It splits the input based on space character and the number of elements we want will be length of that array
Hope it helps :-)
+ 3
♪®!_!b@♪💖
You don't need to write for loop because s and l.length will be always equal because for loop depends on the length of string array.
+ 2
Thank you AJ #Learn With Me 👨💻 yes it works with or without the loop is the same
+ 1
Oh ♪®!_!b@♪💖 thank you, I thought about a loop. Okay I will try this
+ 1
Thank you♪®!_!b@♪💖 it worked