Rechercher : dans
Par :

[Java] test si Int

Dernière réponse le 18 nov 2002 à 13:33:07 steelspirit, le 18 nov 2002 à 08:53:23 
 Signaler ce message aux modérateurs

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 ???

Meilleures réponses pour « [Java] test si Int » dans :
Comment vérifier si mon antivirus est actif ? VoirLe test Si vous avez des doutes sur le fait que votre antivirus soit actif, il vous suffit de télécharger le fichier suivant: http://www.eicar.org/download/eicar_com.zip Si votre antivirus bloque le téléchargement et vous affiche une alerte,...
Java - Les types de données VoirLes primitives Java est un langage orienté objet, c'est-à-dire que les éléments manipulés sont des classes, ou plus exactement des objets, c'est-à-dire des instances de classes. Toutefois ces objets contiennent des données possèdant un type (et...
Java: Les structures conditionnelles VoirQu'est-ce qu'une structure conditionnelle? On appelle structure conditonnelle les instructions qui permettent de tester si une condition est vraie ou non. Ces structures conditionnelles peuvent être associées à des structures qui se répètent...
Langage C++ - Les structures conditionnelles VoirQu'est-ce qu'une structure conditionnelle ? On appelle structure conditionnelle les instructions qui permettent de tester si une condition est vraie ou non. Ces structures conditionnelles peuvent être associées à des structures qui se répètent...

1

Joshua42, le 18 nov 2002 à 09:21:08

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);
}

Répondre à Joshua42

2

choubaka, le 18 nov 2002 à 09:47:14

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

Répondre à choubaka

4

steelspirit, le 18 nov 2002 à 09:53:27

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

Répondre à steelspirit

3

choubaka, le 18 nov 2002 à 09:53:08

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

Répondre à choubaka

5

choubaka, le 18 nov 2002 à 10:09:05

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"

Répondre à choubaka

6

choubaka, le 18 nov 2002 à 10:15:23

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"

Répondre à choubaka

7

 HackTrack, le 18 nov 2002 à 13:33:07

Public boolean isNumber(String aString){
boolean isANumber = true;
try{
Integer.parseInt(aString);
}catch(NumberFormatException nfe){
isANumber = false;
}finally{
return isANumber;
}
}

Répondre à HackTrack
Collection CommentÇaMarche.net