[Java] test si Int

Fermé
steelspirit - 18 nov. 2002 à 08:53
 Utilisateur anonyme - 18 nov. 2002 à 13:33
bonjour tout le monde

Quand j'appel mon prog, je rentre en paramètres des Int : (java Toto 1 12 5)
ces arguments sont interprétés comme des strings(car : le main est :public static void main (String args[])
comment faire pour tester si ces des int ou des string ???
A voir également:

6 réponses

Utilisateur anonyme
18 nov. 2002 à 13:33
public boolean isNumber(String aString){
boolean isANumber = true;
try{
Integer.parseInt(aString);
}catch(NumberFormatException nfe){
isANumber = false;
}finally{
return isANumber;
}
}
2
Joshua42 Messages postés 77 Date d'inscription vendredi 4 janvier 2002 Statut Membre Dernière intervention 18 décembre 2006 8
18 nov. 2002 à 09:21
tu peux faire un truc comme ca par exemple :

private boolean verifNumber(String number)
{
for (int i = 0; i < number.length(); i++)
{
char nb = number.charAt(i);
if ((nb != '0') && (nb != '1') && (nb != '2') && (nb != '3') &&
(nb != '4') && (nb != '5') && (nb != '6') && (nb != '7') &&
(nb != '8') && (nb != '9'))
return (false);
}
return (true);
}
0
choubaka Messages postés 39396 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 8 juin 2024 2 101
18 nov. 2002 à 09:47
salut, il y a plus simple, il n'y a qu'a gérer l'exception

voici une solution possible, en voici le détail de la javadoc

valueOf
public static Integer valueOf(String s)
throws NumberFormatException
Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.
In other words, this method returns an Integer object equal to the value of:

new Integer(Integer.parseInt(s))

Parameters:
s - the string to be parsed.
Returns:
an Integer object holding the value represented by the string argument.
Throws:
NumberFormatException - if the string cannot be parsed as an integer.

--------------------------------------------------------------------------------


Choubanimal : 
"L'alcool est un ennemi", c'est lâche de fuir l'ennemi
0
steelspirit
18 nov. 2002 à 09:53
j'avait déja pensé à ça choubaka... ta solution marche bien si y a que des int ! mais si tu mélanges des int et des strings : java Toto 12 a ba 41
et ben la c pas cool ! donc la première solution me convient bien
0
choubaka Messages postés 39396 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 8 juin 2024 2 101
18 nov. 2002 à 09:53
autre solution tout aussi valable qui est reprise en exemple dans ma première proposition

parseInt
public static int parseInt(String s)
throws NumberFormatException

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

Choubanimal : 
"L'alcool est un ennemi", c'est lâche de fuir l'ennemi
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
choubaka Messages postés 39396 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 8 juin 2024 2 101
18 nov. 2002 à 10:09
re

Et bien alors, tu récupères les caractères du sTring dans un array, il
ne te reste plus qu'à tester caractère par caractère

il existe une classe

StringCharacterIterator(String s) qui te permet de retirer tous les

caractères du String. Que tu peux parcourir, convertir le char en String et avec ce String tester la conversion en chiffre

Une simple itération suffit.

voici le bazar

java.text
Class StringCharacterIterator
java.lang.Object
|
+--java.text.StringCharacterIterator
All Implemented Interfaces:
CharacterIterator, Cloneable

--------------------------------------------------------------------------------

public final class StringCharacterIterator
extends Object
implements CharacterIterator
StringCharacterIterator implements the CharacterIterater protocol for a String. The StringCharacterIterator class iterates over the entire String.



See Also:
CharacterIterator

--------------------------------------------------------------------------------

Field Summary
Fields inherited from interface java.text.CharacterIterator
DONE
Constructor Summary
StringCharacterIterator(String text)
Constructs an iterator with an initial index of 0.
StringCharacterIterator(String text, int pos)
Constructs an iterator with the specified initial index.
StringCharacterIterator(String text, int begin, int end, int pos)
Constructs an iterator over the given range of the given string, with the index set at the specified position.
Method Summary
Object clone()
Creates a copy of this iterator.
char current()
Implements CharacterIterator.current() for String.
boolean equals(Object obj)
Compares the equality of two StringCharacterIterator objects.
char first()
Implements CharacterIterator.first() for String.
int getBeginIndex()
Implements CharacterIterator.getBeginIndex() for String.
int getEndIndex()
Implements CharacterIterator.getEndIndex() for String.
int getIndex()
Implements CharacterIterator.getIndex() for String.
int hashCode()
Computes a hashcode for this iterator.
char last()
Implements CharacterIterator.last() for String.
char next()
Implements CharacterIterator.next() for String.
char previous()
Implements CharacterIterator.previous() for String.
char setIndex(int p)
Implements CharacterIterator.setIndex() for String.
void setText(String text)
Reset this iterator to point to a new string.



Choubanimal : 
"Le poilu poilant au poil"
0
choubaka Messages postés 39396 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 8 juin 2024 2 101
18 nov. 2002 à 10:15
re re

l'avantage de cette manière de procéder, c'est que le traitement ultérieur des données est plus aisé

puisque une seule condition est nécessaire

Choubanimal : 
"Le poilu poilant au poil"
0