[Java] test si Int
steelspirit
-
Utilisateur anonyme -
Utilisateur anonyme -
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 ???
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:
- Is int java
- What is my movie français - Télécharger - Divers TV & Vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Waptrick java football - Télécharger - Jeux vidéo
- Waptrick java voiture - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
6 réponses
public boolean isNumber(String aString){
boolean isANumber = true;
try{
Integer.parseInt(aString);
}catch(NumberFormatException nfe){
isANumber = false;
}finally{
return isANumber;
}
}
boolean isANumber = true;
try{
Integer.parseInt(aString);
}catch(NumberFormatException nfe){
isANumber = false;
}finally{
return isANumber;
}
}
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);
}
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);
}
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
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
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
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
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
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"
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"