r/javahelp • u/Dishy2001 • Mar 09 '20
Homework How to put a max character length on a string
Im doing a college assignment and cannot figure how to put a max character length of 40 on a String
20
Upvotes
2
u/fsbk366 Mar 10 '20
If the string is a parameter of an object, you can add limiting criteria to the setter method.
15
Mar 09 '20
[removed] — view removed comment
3
u/Dishy2001 Mar 09 '20
Thank you, that thought never popped into my head. Will use that as its relitively straight forward. Thanks a million
4
u/Python4fun Mar 10 '20
You can also check the string length and throw an error if the length is too high.
2
u/AssassinBoy49 Nooblet Brewer Mar 12 '20
you can use the length method and an if statement to check that it is within the limit. if not the program should produce an error