|
20 Oct 2006, 09:58
|
#1
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
[Java] Ignoring Letters
Is there a way to ignore letters when entered into an input that is storing the value as an integer? (if someone accidentally enters letters into the input) If so, a way to repeat the input message if letters were accidentally included?
There's an e.g. ans.equalsIgnoreCase statement but I can't find one for IgnoreLetters... ?
Thanks.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
20 Oct 2006, 10:02
|
#2
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: [Java] Ignoring Letters
Also, how do you add more than one statement in the 'or' expression?
e.g.
I can do 2 statements so that only if both statements are true:
if (vote_three > vote_one && vote_three > vote_two)
but how would I do e.g. vote_three > vote_one OR vote_three > vote_two in the above statement and in the statement below?
if (zerovoters.equals("yes"))
^^ e.g. for above - if zerovoters == "yes or y" if you see what I mean?
Thanks.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
20 Oct 2006, 10:38
|
#3
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: [Java] Ignoring Letters
1) enclose it in a try-catch block, catch the NumberFormatException and make them enter it again if caught.
for instance
Code:
int input = 0;
boolean valid = false;
do
{
try
{
input = Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"gimmie a number")); // woo gui input! tho you can do this however you want
}
catch (NumberFormatException ex)
{
javax.swing.JOptionPane.showMessageDialog(null,"thats not a number");
continue;
}
// if we get here, its valid
valid = true;
}
while ( !valid );
2) use the || , luke!
|| = logical OR
&& = logical AND
putting a ! in front of a parameter makes it check if the inverse is true
for instance : if ( !water.equals("wet") )
would only trigger when water is not wet
if ( vote_three > vote_one || vote_three > vote_two )
if ( zerovoters.equals("yes") || zerovoters.equals("y") )
etc is what you want
it might be better to check to see if the first letter equals y though - as that would catch both cases with one stone
Last edited by Phil^; 20 Oct 2006 at 10:52.
|
|
|
20 Oct 2006, 10:41
|
#4
|
Emperor
Join Date: Jul 2001
Location: in front of a computer
Posts: 490
|
Re: [Java] Ignoring Letters
Quote:
Originally Posted by Phil^
it might be better to check to see if the first letter equals y though - as that would catch both cases with one stone
|
And also covers "Yeah", "Yay" etc.
/me cries
|
|
|
24 Oct 2006, 09:36
|
#5
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: [Java] Ignoring Letters
Thanks guys
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
26 Oct 2006, 13:50
|
#6
|
Registered User
Join Date: Jun 2004
Posts: 7
|
Re: [Java] Ignoring Letters
has nothing to do with your problem, but:
Quote:
Originally Posted by Androme2
if (zerovoters.equals("yes"))
|
is not a good idea
make it:
if ("yes".equals(zerovoters))
alway do it constant.equals(variable) NOT variable.equals(constant)
why?
if the variable is null, u will get an nullpointerexception, other way round it will say its not equal.
gl, Floi
__________________
R 9.5_ 34:4:5_ rank ??? - gal rank 31 - Thx to all my great gal mates
R 11__26:2:2_ rank 191 - gal rank 36 - first NoS, then ND
R 12__11:2:10 rank _69 - gal rank 35 - ND, ally #3
R 13__9:3:13_ rank 580 - gal rank 28 - no ally for most time
R 14__10:1:14 rank 106 - gal rank 21 - ND, ally #2
R 15 ???
R 16 ???
R 17 ???
R 18 ???
R 19___6:10:12 rank _94 - gal rank ?? - ND, ally #2
R 20 ???
R 21 ???
|
|
|
26 Oct 2006, 14:02
|
#7
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: [Java] Ignoring Letters
getting a null pointer exception is a good thing. Sometimes you want to be able to determine if something has gone seriously wrong with user input or otherwise, and catch it.
Its like an added signal - " its not only not equal, its nonexistant. dangerous to use this variable in further code. throw a wobby and demand it be fixed"
For instance, if in the example - when they are not equal it calls the objects .toString() method, it would still throw a null pointer exception but it wouldnt have been caught until that point which could be several objects and methods in, having been copied to numerous other objects as a reference etc etc
|
|
|
26 Oct 2006, 14:40
|
#8
|
Registered User
Join Date: Jun 2004
Posts: 7
|
Re: [Java] Ignoring Letters
Exceptions in gerneral are not a bad thing. And if u handle them - everything is fine.
Just thought in the example above it was just an input that should be checked if its "yes" or not and then thrown away. Of course u need to handle it, if u need the variable later. And the earlier u do, the better.
Just wanted to point out, that he might get in problems, if he does it the way it was posted - and there was no hint for him to put a try-catch around it. (or at least i didnt see it)
__________________
R 9.5_ 34:4:5_ rank ??? - gal rank 31 - Thx to all my great gal mates
R 11__26:2:2_ rank 191 - gal rank 36 - first NoS, then ND
R 12__11:2:10 rank _69 - gal rank 35 - ND, ally #3
R 13__9:3:13_ rank 580 - gal rank 28 - no ally for most time
R 14__10:1:14 rank 106 - gal rank 21 - ND, ally #2
R 15 ???
R 16 ???
R 17 ???
R 18 ???
R 19___6:10:12 rank _94 - gal rank ?? - ND, ally #2
R 20 ???
R 21 ???
|
|
|
26 Oct 2006, 14:51
|
#9
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: [Java] Ignoring Letters
true, it would be much better in a try catch block
Rule number one of user input : the users are stupid. Never underestimate the power of stupidity in large numbers.
|
|
|
28 Oct 2006, 04:43
|
#10
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: [Java] Ignoring Letters
Quote:
Originally Posted by Floi
make it:
if ("yes".equals(zerovoters))
alway do it constant.equals(variable) NOT variable.equals(constant)
if the variable is null, u will get an nullpointerexception, other way round it will say its not equal.
|
Can't I just make a new public and create my own error message? While I understand your formatting, it's just confusing me.
According to the Sun API, I can make a new public and set my own message if the variable is returned as being null.
public NullPointerException(String s)
Constructs a NullPointerException with the specified detail message.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
28 Oct 2006, 04:45
|
#11
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: [Java] Ignoring Letters
Btw, is there a way to ignore numbers in an input (only accept letters)?
Would I use the same try and catch principle?
Thanks.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
28 Oct 2006, 12:12
|
#12
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Re: [Java] Ignoring Letters
Use a regular expression in that instance. Something like ^[\w|\s]*$ maybe but I've done no regex stuff in years so someone else round here'd be better for that.
__________________
You're now playing ketchup
|
|
|
28 Oct 2006, 12:59
|
#13
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: [Java] Ignoring Letters
or you could use a for loop to break the string down into chars and compare each one using Character.isDigit or Character.isLetter , as defined in http://java.sun.com/j2se/1.3/docs/ap...Character.html
use String.charAt() to get a char from a string
Its not a good idea to create your own exception class which overrules a default one, so dont use the name NullPointerException for one.
A try-catch here wont work unless you create a compare function which throws a custom defined exception like NotALetterException , that you create yourself
|
|
|
31 Oct 2006, 10:28
|
#14
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: [Java] Ignoring Letters
thanks
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
17 Nov 2006, 00:19
|
#15
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: [Java] Ignoring Letters
I'm having some problems
Code:
do {
for (int i=1; i<2; i++)
{
answer[0] = JOptionPane.showInputDialog(question[0]);
if (answer[0].equalsIgnoreCase("yes"))
{
answer[2] = JOptionPane.showInputDialog(question[2]);
}
else if (answer[0].equalsIgnoreCase("no"))
{
answer[5] = JOptionPane.showInputDialog(question[5]);
}
else
{
for (int j=1, j<=3, j++)
{
answer[0].charAt(j)
if (Character.isDigit(answer[0]))
{
JOptionPane.showMessageDialog(null, "Number entered.");
}
else
{
JOptionPane.showMessageDialog(null, "You did not enter yes or no.\n\n Please go back and try again."); j--;
}
}
}
}
} while (!answer[0].equalsIgnoreCase("yes") || !answer[0].equalsIgnoreCase("no"));
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Last edited by Androme; 17 Nov 2006 at 00:27.
|
|
|
|
All times are GMT +1. The time now is 04:13.
| |