How to find string range

Say i have a string array from aa0 to aa100. Now how do i write a condition to check that this string lies between aa0 to aa100. please help
[147 byte] By [ysrpa] at [2007-11-27 5:11:41]
# 1
What about aA0?Check String.compareTo, or check substring and Integer.parseInt. Depending on your requirements.
CeciNEstPasUnProgrammeura at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 2
Quick and dirty solution: Iterate through your arrays i.e., use for-loop. Then check value of your string against your array value. If the iteration ends without match, then your string is not within your array range.
eadelarosaa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 3

This is the code what i have typed. Its working but when i enter a value which is less then 0 and greater than 99, it still works, where as it shouldnt. Please help

public void focusLost(FocusEvent evt) {

if(evt.getSource() == mytextfield) {

System.out.println("Inside focuslost");

String text = mytextfield.getText();

if(text.startsWith("aa")) {

int value;

try {

value = Integer.parseInt(String.valueOf(text.charAt(text.length() - 1)));

}catch(NumberFormatException nfe) {JOptionPane.showMessageDialog(null,"textfield should be \n in the range of aa0 - aa99","Test",JOptionPane.ERROR_MESSAGE);return;}

if(value < 0 || value > 99) {

System.out.println("Inside if condition");

JOptionPane.showMessageDialog(null,"textfield should be \n in the range of aa0 - aa99","Test",JOptionPane.ERROR_MESSAGE);

mytextfield.transferFocus();

return;

}

}

else {

JOptionPane.showMessageDialog(null,"Invalid entry","Test",JOptionPane.ERROR_MESSAGE);

return;

}

}

}

ysrpa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 4
> value = Integer.parseInt(String.valueOf(text.charAt(text.length() - 1)));This only reads 1 char. What you want to do is read from index 2 to the end.
quittea at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for the tips. I have changed my code as follows

value = Integer.parseInt(String.valueOf(text.substring(text.lastIndexOf('a')+1,text.length())));

...

Also changed if condition to :

if(value < 0 && value > 99)

But still i get the same problem. If i enter values less 0 and greater than 99 its not entering if condition.

ysrpa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 6
> Also changed if condition to :> if(value < 0 && value > 99)Undo this. A value can't be less than 0 and greater than 99 at the same time.
quittea at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 7
> > Also changed if condition to :> > if(value < 0 && value > 99)> > Undo this. A value can't be less than 0 and greater> than 99 at the same time.True. I understood. But please give me tips on how to check that
ysrpa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 8
> True. I understood. But please give me tips on how to check that conditionYour first approach to this was pretty good.
quittea at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 9
Me bad. I got confused. That should || (or) instead of &&(and) operator. Thank you so much.
ysrpa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 10

I have one more doubt. Is mytextfield.requestFocus() correct. Actually on error i want the cursor to go back to mytextfield. But it seems its not working. I dont see cursor blinking in mytextfield. I strictly dont want the cursor to move out of mytextfield untill i enter proper correct value. How can i do this? Please help me

ysrpa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 11
Imho, requestFocus() _should_ work. However, your code says "transferFocus()" -- are you sure about that?
quittea at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 12

> Imho, requestFocus() _should_ work. However, your

> code says "transferFocus()" -- are you sure about

> that?

Yeah i changed it to requestFocus(). And its keep displaying error message three times. Even if i move to other application i get error messages. How can i display error message only once and within that application only if i click on other other fields when value is wrong.

ysrpa at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 13

> Yeah i changed it to requestFocus(). And its keep

> displaying error message three times.

Make sure you didn't register your listener three times (we never know...)

> Even if i move to other application i get error messages.

> How can i display error message only once and within that

> application only if i click on other other fields

> when value is wrong.

From the [url=http://java.sun.com/javase/6/docs/api/java/awt/event/FocusEvent.html#isTemporary()]API doc[/url]:

There are two levels of focus events: permanent and temporary. Permanent focus change events occur when focus is directly moved from one Component to another, such as through a call to requestFocus() or as the user uses the TAB key to traverse Components. Temporary focus change events occur when focus is temporarily lost for a Component as the indirect result of another operation, such as Window deactivation or a Scrollbar drag. In this case, the original focus state will automatically be restored once that operation is finished, or, for the case of Window deactivation, when the Window is reactivated. Both permanent and temporary focus events are delivered using the FOCUS_GAINED and FOCUS_LOST event ids; the level may be distinguished in the event using the isTemporary() method.

TimTheEnchantora at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 14

Try passing your focus request to the event queue:

SwingUtilities.invokeLater(new Runnable() {

public void run() {

mytextfield.requestFocus();

}

});

quittea at 2007-7-12 10:32:08 > top of Java-index,Java Essentials,Java Programming...
# 15

> Try passing your focus request to the event queue:

> > SwingUtilities.invokeLater(new Runnable() {

>public void run() {

>mytextfield.requestFocus();

> }

> });

>

I didnt understand what you mean. You mean i have call invokeLater method in the place requestFocus() call.

ysrpa at 2007-7-21 21:21:46 > top of Java-index,Java Essentials,Java Programming...
# 16
> From the API doc ...Right, isTemporary() should be checked, too. Event number three is somehow produced by the JOptionPane popping up, see my last response for this.
quittea at 2007-7-21 21:21:46 > top of Java-index,Java Essentials,Java Programming...
# 17

> > Yeah i changed it to requestFocus(). And its keep

> > displaying error message three times.

>

> Make sure you didn't register your listener three

> times (we never know...)

>

No i have registered only once

> > Even if i move to other application i get error

> messages.

> > How can i display error message only once and

> within that

> > application only if i click on other other fields

> > when value is wrong.

>

> From the

> [url=http://java.sun.com/javase/6/docs/api/java/awt/ev

> ent/FocusEvent.html#isTemporary()]API doc[/url]:

> There are two levels of focus events: permanent and

> temporary. Permanent focus change events occur when

> focus is directly moved from one Component to

> another, such as through a call to requestFocus() or

> as the user uses the TAB key to traverse Components.

> Temporary focus change events occur when focus is

> temporarily lost for a Component as the indirect

> result of another operation, such as Window

> deactivation or a Scrollbar drag. In this case, the

> original focus state will automatically be restored

> once that operation is finished, or, for the case of

> Window deactivation, when the Window is reactivated.

> Both permanent and temporary focus events are

> delivered using the FOCUS_GAINED and FOCUS_LOST event

> ids; the level may be distinguished in the event

> using the isTemporary() method.

Ok i understood this. But how can this help me to solve my problem

ysrpa at 2007-7-21 21:21:46 > top of Java-index,Java Essentials,Java Programming...
# 18
> I didnt understand what you mean. You mean i have> call invokeLater method in the place requestFocus() call.Right. Replace your focus requesting line by the snippet I posted. Additionally, see what Tim posted on temporary focus lost events.
quittea at 2007-7-21 21:21:46 > top of Java-index,Java Essentials,Java Programming...
# 19

> Ok i understood this. But how can this help me to solve my problem

Change

if(evt.getSource() == mytextfield) {

to

if ((evt.getSource() == mytextfield) && (!evt.isTemporary())) {

and

mytextfield.requestFocus();

to

SwingUtilities.invokeLater(new Runnable() {

public void run() {

mytextfield.requestFocus();

}

});

That should do it.

quittea at 2007-7-21 21:21:46 > top of Java-index,Java Essentials,Java Programming...
# 20
Brilliant!! Both of your tips worked. @Tim: Thank you@quitte: Thank you so much
ysrpa at 2007-7-21 21:21:46 > top of Java-index,Java Essentials,Java Programming...