How can I take input string of fixed length in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I take input string of fixed length in java?

10th Feb 2021, 7:29 AM
Vishakha Shrivastav
Vishakha Shrivastav - avatar
4 Answers
+ 2
public class Program { public static void main(String[] args) { String n="atul"; int h=n.length(); if(h<5){ System.out.print ("yes"); } else{ System.out.print ("error"); } } }
10th Feb 2021, 8:06 AM
Atul [Inactive]
+ 2
Vishakha Shrivastav There is no any other options because String can't be of fixed length. So if you want that user only enter fixed length of string then you have to check its length and throw message if it is greater than fixed length. See the example of Atul
10th Feb 2021, 9:35 AM
A͢J
A͢J - avatar
+ 1
Vishakha Shrivastav Please if you can give one example
10th Feb 2021, 8:27 AM
Atul [Inactive]
0
Like this??
10th Feb 2021, 8:06 AM
Atul [Inactive]