Java:method append(char) undefined for String

domxaline -  
 domxaline -
Bonjour,
prg suivant donnes erreur de compilation
The method append(char) is undefined for the type String

at IntArray.main(IntArray.java:11)

la ligne 11 est:
dest.append(palindrome.charAt(i));
public class IntArray 
{
 public static void main (String[]args)
 { 
	 String palindrome=new String("Rod saw i was dor");
	 int len=palindrome.length();
	 String dest=new String();
	 for(int i=(len-1);i>=0;i--)
	 {
		 dest.append(palindrome.charAt(i));
	 }
	 System.out.format("%s%n",dest.toString());
 }
}


A voir également:

5 réponses

Thyjukil
 
À ce que je sache, il n'existe pas de méthode append() pour le type String ; mais il y en a un pour le type StringBuffer.
Si tu veux utiliser cette méthode, essaye de convertir ta chaîne en StringBuffer (StringBuffer sb = new StringBuffer(dest); )
0
Utilisateur anonyme
 
Salut,

dest.append(palindrome.charAt(i));

L'équivalent de append qui n'existe pas pour String serait +=
soit remplacer:
dest.append(palindrome.charAt(i));
par:
dest+= palindrome.charAt(i);

Cordialement,

Dan
0
domxaline
 
j'ai une question bête
resultat pour ce prg est ainsi:
rod saw i was doR

pourquoi la deriner lettre est en majuscule doR
public class IntArray  
{ 
 public static void main (String[]args) 
 {  
  String palindrome=new String("Rod saw i was dor"); 
  int len=palindrome.length(); 
  String dest=new String(); 
  for(int i=(len-1);i>=0;i--) 
  { 
   //dest.append(palindrome.charAt(i)); 
   dest+= palindrome.charAt(i);  
  } 
  System.out.format("%s%n",dest.toString()); 
 } 
} 
0
Utilisateur anonyme
 
Re,
Parcequ'en fait, avec for(int i=(len-1);i>=0;i--) on copie "Rod saw i was dor" caractère par caractère en commencant par la fin dans dest soit r puis o puis d puis space puis s, a, w, etc.. pour terminer par d, o et R.

Donc, on inverse les caractères de "Rod saw i was dor" en commencant par la fin pour obtenir "rod saw i was doR".

Pour que ce soit un palindrome il faut que ce soit une phrase "syntaxiquement" correcte.
0

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

Posez votre question
domxaline
 
merci beaucoup
vos aides était très utile
0