Java String Limit

I am new to java (previously worked only with sql) and I am trying to set a length limit for my string variable. Basically, I have a username field that can be only 6 characters long.

I am trying to do the following:

private String username (6); 

I assume this is not the correct format. Does anyone know how I can do this in java correctly?

+4
source share
7 answers

Some other answers claim that "there is no way to limit the lines in java to a certain finite number using built-in functions" and suggested using your own. However, the Java EE validations API is just for that. Example:

 import javax.validation.constraints.Size; public class Person { @Size(max = 6) private String username; } 

For more information on how to use the validation API, see, for example, this thread . Hibernate validator is a reference implementation ( use ).

In short, when annotating an object like @Valid, checks made in annotations will be performed.

+4
source

What you suggested is not the right way to do what you want. Try using:

 private int stringLimit = 6; // Take input from user private String username = inputString.substring(0,stringLimit); 

For instance:

 inputString = "joelspolsky"; private String username = inputString.substring(0,stringLimit); // username is "joelsp" 
+2
source

You can try the following: perform user input, then check this line using the following function.

 String output =""; public boolean set(String str, int limit){ if(str.length() <= limit){ output= str; return true; } else return false; } 
+1
source

It is not possible to limit strings in java to a specific finite number using built-in functions. Strings are immutable and take on the value that you provide in your constructor. To do this, you will need to write the code manually.

Use the length() function to determine the length of a string and do not allow lengths greater than 6.

 if( username.length() > 6 ) { throw new RuntimeException("User name too long"); } 

One option you have is to throw an exception and then handle it elsewhere. Or you can immediately notify the user about how this problem arose.

+1
source

In this case, the annotation mechanism can be useful if, of course, you know what it is.

You can create your own annotation, for example:

 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface MaxLength { int value(); } 

And use it like:

 @MaxLength(6) private String username; 

Then you need to post-process such objects in a special post-processor, which you must create manually.

0
source

SubString () is not suitable for this. If the input string is less than the limit, a StringIndexOutOfBoundsException is thrown . I think you can use StringBuilder for this.

 StringBuilder buider = new StringBuilder(username); builder.setLength(6); String restName = builder.toString().trim(); 
0
source

example for shortening URL length

 if (getURLitem.length() >= 15) { int stringLimit = 15; final String smallURL = getURLitem.substring(0, stringLimit); //show short string in textview... TextView urlLink = (TextView) findViewById(R.id.url_link); urlLink.setText(smallURL); // Set On click listener and open URL below ........... } else { //show full string in textview... TextView urlLink = (TextView) findViewById(R.id.url_link); urlLink.setText(getURLitem); // Set On click listener and open URL below ........... } 
-2
source

All Articles