Java test palindrome

duke -  
 AnouarTunisiano -
slt
je debute total en java, j'ai fais ça :
import java.io.*;

class Palin{




static public void main(String argv[]) throws java.io.IOException{


String mot;

System.out.println("tapez le mot");
System.out.flush();

//on recup le mot taper
mot=new DataInputStream( System.in).readLine();
int mottab[]= new int[mot.length()];

System.out.println(mot.charAt(1));
System.out.println(mot.length());

//on met lengthmot taper dans un tablo
for (int i=0; i<=mot.length(); i++){
System.out.println( i);



mottab[i] = mot.charAt(i);
System.out.println( "ouai");
System.out.println( mottab[i]);
}

//on va comparer les lettres debut fin en revenant vers le milieu
for (int j=0; j<mot.length(); j++){

if(mottab[j]==mottab[mot.length()-j]){}
else{
System.out.println("ce nest pas un palin");
break;
}
}

System.out.println("trop bon, cela est un palin");
}



}


ça doit me dire si un mot et un palindrome ou pas, un palindrome cKAYAK ou BOB, un mot qui se lit dans les deux sens


merci pour votre aide
A voir également:

11 réponses

AnouarTunisiano
 
Salut,
Voilà votre code rectifié :


import java.io.*;
class Palin{
static public void main(String argv[]) throws java.io.IOException{
String mot;
boolean bo=true;
System.out.println("tapez le mot");
System.out.flush();
//on recup le mot taper
mot=new DataInputStream( System.in).readLine();
int mottab[]= new int[mot.length()];
//System.out.println(mot.charAt(1));
//System.out.println(mot.length());
//on met lengthmot taper dans un tablo
for (int i=0; i<mot.length(); i++)
//System.out.println( i);
mottab[i] = mot.charAt(i);
/*System.out.println( "ouai");
System.out.println( mottab[i]);
}*/
//on va comparer les lettres debut fin en revenant vers le milieu
for (int j=0; j<mot.length()/2; j++){

//System.out.println(mottab[j]);
//System.out.println(mottab[mot.length()-j-1]);

if(mottab[j]==mottab[mot.length()-j-1]){}
else
{
bo=false;
System.out.println("ce nest pas un palin");
break;
}
}
if(bo==true)
System.out.println("trop bon, cela est un palin");
}
}



J'espère que ça vous aidera
@+
5
Nettogrof Messages postés 521 Date d'inscription   Statut Membre Dernière intervention   672
 
je crois savoir:

exemple avec le mot Kayak

mot.length == 5
dont int mottab[] = new int[5] // dont mottab[0] à mottab[4]

mais ta premiere boucle (for( int i=0; i<=mot.length(); i++){ )
ton i commence a 0 jsuqu'a 5 car i<=mot.length()
et mottab[5] est impossible...

esssaye ta 1ere boucle
for(int i=0; i<mot.length(); i++){


Nettogrof tseb era seiromem emoS
1
duke
 
import java.io.*;
import java.lang.*;
class Palin{




static public void main(String argv[]) throws java.io.IOException{


String mot;

System.out.println("tapez le mot");
System.out.flush();

//on recup le mot taper
mot=new DataInputStream( System.in).readLine();
int []mottab;
mottab= new int[mot.length()];
System.out.println("mot="+mot);
System.out.println(mot.charAt(1));
System.out.println(mot.length());

//on met lengthmot taper dans un tablo
for (int i=0; i<mot.length(); i++){
System.out.println( i);
mottab[i] = mot.charAt(i);
//System.out.println( "ouai");
System.out.println((char) mottab[i]);
}

//on va comparer les lettres debut fin en revenant vers le milieu
for (int j=0; j<mot.length()/2; j++){

System.out.println("----------------");
System.out.println((char) mottab[j]);
System.out.println( (char) mottab[(mot.length()-j)] );

if((char) mottab[j] == (char) mottab[mot.length()-j]){}
else{
System.out.println("ce nest pas un palin");
break;
}
}

System.out.println("trop bon, cela est un palin");
}



}


maintenant c bon j'ai mi les lettre du mot dans le tablo mais (char) mottab[mot.length()-j] chie :(
1
Nettogrof Messages postés 521 Date d'inscription   Statut Membre Dernière intervention   672
 
Et quel est ta question?

a premiere vue, ca semble correct...


Nettogrof tseb era seiromem emoS
0

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

Posez votre question
duke
 
non ça chie sur : mottab[i] = mot.charAt(i);

java.lang.ArrayIndexOutOfBoundsException: 3
at Palin.main(Palin.java:35)
Exception in thread "main" 


:(
0
Marden Messages postés 1072 Date d'inscription   Statut Membre Dernière intervention   210
 
On peut arrêter le test à la moitié des lettres (division entière par 2, puisque si le nombre de lettres est impair, la lettre centrale n'a pas lieu d'être testée). Attention aussi à la casse ("K" != "k") !
0
duke
 
oaui je sais ja coriger ça avant vos poste ;) mais le probleme est tjs la
import java.io.*;
import java.lang.*;
class Palin{




static public void main(String argv[]) throws java.io.IOException{


String mot;

System.out.println("tapez le mot");
System.out.flush();

//on recup le mot taper
mot=new DataInputStream( System.in).readLine();
int []mottab;
mottab= new int[mot.length()];
System.out.println("mot="+mot);
System.out.println(mot.charAt(1));
System.out.println(mot.length());

//on met lengthmot taper dans un tablo
for (int i=0; i<mot.length(); i++){
System.out.println( i);
mottab[i] = mot.charAt(i);
//System.out.println( "ouai");
System.out.println(mottab[i]);
}

//on va comparer les lettres debut fin en revenant vers le milieu
for (int j=0; jmot.length()/2; j++){

if(mottab[j]==mottab[mot.length()-j]){}
else{
System.out.println("ce nest pas un palin");
break;
}
}

System.out.println("trop bon, cela est un palin");
}



}


quand je print System.out.println( mottab[i]); il me met 108 ou qqch comme ça Oô
0
duke
 
c bon fallais juste mettre j-1 car dans le tablo on part de 0 eheheh :$
0
Nettogrof Messages postés 521 Date d'inscription   Statut Membre Dernière intervention   672
 
Simple question:

Pourquoi un tableau d'entier int mottab[]= new int[mot.length]; ?

un tableau de char n'aurait-il pas été plus simple?

Nettogrof tseb era seiromem emoS
0
duke
 
import java.io.*;
import java.lang.*;

class Palin{




static public void main(String argv[]) throws java.io.IOException{


String mot;

System.out.println("tapez le mot");
System.out.flush();

//on recup le mot taper
mot=new DataInputStream( System.in).readLine();



System.out.println("-------------------");
//on va comparer les lettres debut fin en revenant vers le milieu
for (int j=0; j<mot.length()/2; j++){

//on parcour les caratere depuis les extremité en allant vers lengthmilieu du mot
if(mot.charAt(j) == mot.charAt(mot.length() - j -1)){

//si on est au milieu du mot, on affiche le resultat
if(j==(mot.length()/2)-1){
System.out.println("cela est un palin");
break;
}
}

else{ //si les caracteres oposés sont pas égale, on a pas un palindrome
System.out.println("ce nest pas un palin");
break;
}

}


}

}


//ça marche ça, en faite le tablo servait a kdal :) mais bon je pouvais pas savoir
0
btissam
 
// palendrom
import java.io.DataInputStream;

class palendrom{

@SuppressWarnings("deprecation")

static public void main(String argv[]) throws java.io.IOException{
String mot;

System.out.println("tapez le mot");
System.out.flush();

//on recup le mot taper

mot=new DataInputStream( System.in).readLine();

System.out.println("-------------------");
//on va comparer les lettres debut fin en revenant vers le milieu

for (int j=0; j<mot.length()/2; j++){
//on parcour les caratere depuis les extremité en allant vers lengthmilieu du mot

if(mot.charAt(j) == mot.charAt(mot.length() - j -1)){

//si on est au milieu du mot, on affiche le resultat

if(j==(mot.length()/2)-1){

System.out.println("cela est un palin");
break;
}
}
else{ //si les caracteres oposés sont pas égale, on a pas un palindrome
System.out.println("ce nest pas un palin");

break;
}
}

}
}
************************************************************************ tapez le mot
kolm
-------------------
ce nest pas un palin
tapez le mot
laval
-------------------
cela est un palin

j ai pas bien comprit le programme" les phrase qui sont souligne"
0
choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
salut

je vais mettre un peu de sel dans l'histoire....

pourquoi de pas utiliser les structures de données fournies par java, comme par exemple les ArrayList? huh? beaucoup plus malébables, moins statiques et ce sont des objets à part entière..

ok je sors

Chouba,
Tatatatala...  Chi Hua Hua!
0