Hi,
is there a way for me to limit the length of the String entered to one ?
Thanks.
Limit String entry length
Started by Xdawn90, Jul 08 2009 10:53 PM
5 replies to this topic
#1
Posted 08 July 2009 - 10:53 PM
|
|
|
#2
Posted 08 July 2009 - 11:27 PM
Do you want to limit it for other programmer or user?
i think there's no simple way - think of all the methods that could fail because of this limit.
You can write a class that grants access to the String.
i think there's no simple way - think of all the methods that could fail because of this limit.
You can write a class that grants access to the String.
class LimitedString{
private String str;
private int limit;
public LimitedString(int limit){
this.limit = limit;
}
public boolean set(String str){
if(str.length() <= limit){
this.str = str;
return true;
}
else return false;
}
public String toString(){
return str;
}
}
Edited by Sinipull, 09 July 2009 - 12:54 AM.
#3
Posted 09 July 2009 - 08:48 AM
Thanks for the suggestion. I will try it out.
I have another question:
How to allow the user to enter a string with spaces in between by using scanner ?
Help is much appreciated. Thanks.
I have another question:
How to allow the user to enter a string with spaces in between by using scanner ?
Help is much appreciated. Thanks.
#4
Posted 09 July 2009 - 09:02 AM
You can use substring
string.substring(start, end);
#5
Posted 09 July 2009 - 04:37 PM
Can you show me the way to do it ?
Thanks.
Thanks.
#6
Posted 10 July 2009 - 09:13 AM
I did, string is the string name, start and end are both ints based on where you want to start or end, the beginning of the string is 0 and the last would be string.length()


Sign In
Create Account


Back to top










