Passage d'un string vers un int.... ou comment savoir si c possible

Passage d'un string vers un int.... ou comment savoir si c possible - Java - Programmation

Marsh Posté le 05-06-2006 à 18:14:00    

je sais que pour passer un string en int c:
 

Citation :

int entier=Integer.parseInt(String chaine);


 
le probleme est que chaine n'est pas obligatoirement un nombre.
 
je cherche donc un moyen de faire:
 

Citation :


if(chaine.estUnEntier())
{
     int entier=Integer.parseInt(chaine);
}
else
{
     int entier=-1; //ce qui signifierai erreur;
}


 
sauf que chaine.estUnEntier().... n'existe pas....
 
Merci beaucoup

Reply

Marsh Posté le 05-06-2006 à 18:14:00   

Reply

Marsh Posté le 05-06-2006 à 18:30:55    

pi etre :

Code :
  1. int entier = 0;
  2. try {
  3.   entier=Integer.parseInt(chaine);
  4. } catch (NumberFormatException e) {
  5.   entier = -1;
  6. }


 
Par contre, l'init de entier a 0, c'est peut etre pas genial

Message cité 1 fois
Message édité par zapan666 le 05-06-2006 à 18:31:10

---------------
my flick r - Just Tab it !
Reply

Marsh Posté le 05-06-2006 à 18:33:28    

merci beaucoup...  
j'avé une solution très usine a gaz.... j'avais oublié le try et catch... je cherchais plus une fonction.
 
je vais essayer ca tout de suite. :jap:

Reply

Marsh Posté le 05-06-2006 à 18:37:50    

Sinon on peut le faire avec des expressions rationnelles :o


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 05-06-2006 à 18:42:04    

Le try et le catch marche tres bien merci. (un manque d'habitude)
 
sinon c koi  

masklinn a écrit :

des expressions rationnelles :o


 
ca m'interesse bcp.

Reply

Marsh Posté le 05-06-2006 à 18:44:15    

http://java.sun.com/docs/books/tut [...] index.html


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 05-06-2006 à 18:54:06    

ou mieux, un NumberFormat, comme ca on peut gerer la localisation des entiers, genre les ricains utilisent la virgule comme séparateur de milliers, pas les francais, etc etc :o
 

Code :
  1. NumberFormat.getIntegerInstance(Locale inLocale)


 
[:dawao]

Reply

Marsh Posté le 18-12-2007 à 21:21:55    

zapan666 a écrit :

pi etre :

Code :
  1. int entier = 0;
  2. try {
  3.   entier=Integer.parseInt(chaine);
  4. } catch (NumberFormatException e) {
  5.   entier = -1;
  6. }


 
Par contre, l'init de entier a 0, c'est peut etre pas genial


 
C'est super Je cherche également la solution, mais il faut inclure quel librairie ?
 
Merci d'avance
 
Me*** c'est du java :(
 
 je cherche en C++ domage :(


Message édité par raph1123 le 18-12-2007 à 21:23:48
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed