ورود

View Full Version : سوال: مشکل در کد بزرگترین



kingtak
سه شنبه 04 تیر 1392, 13:58 عصر
سلام دوستان
من یه برنامه نوشتم که قرار یه فایل متنی رو به عنوان ورودی بگیره و بزرگترین کلمه ای که palindrome هستش رو چاپ کنه.( palindrome منظور کلمه ای که از هر طرف بخونیش یکی باشه مثل :"درد"، "madam").
ولی نمیدونم چرا بعضی وقت ها چند تا کلمه رو با هم میاره
این هم کدش:
public static void main(String[] args) {
Scanner fileIn=null;

try{
fileIn=new Scanner(new FileInputStream("c:\\pp.txt"));
}catch (Exception e) {
System.out.println("File not found!");}
//
String max=fileIn.next();
while (fileIn.hasNext())
{
String word=fileIn.next();
if(isPalindrome(word) && word.length()>max.length())
{
max=word;
System.out.println(max);
}
}
}
public static Boolean isPalindrome(String word) {
if(word.equals(" ") || word.length() < 2 ) return true;
if(word.substring(0,1).equalsIgnoreCase(word.subst ring(word.length()-1)))
return isPalindrome(word.substring(1,word.length()-1));
else
return false;
}

مثلا این کلمات رو بهش میدم:



dad
baba
ana
xoxan
madam
civic
Dewed
deified
mom
Wassamassaw
devoved
Hannah
peeweep
Malayalam
pop
rotavator



خروجی:




madam
deified
Wassamassaw

ولی وقتی یه کلمه بزرگتر رو اول لیست میذارم درست کار میکنه.

AfshinHonari
چهارشنبه 05 تیر 1392, 01:11 صبح
سلام
دوست عزیز اونجا که نوشتی:
System.out.println(max);
این تیکه رو بیرون حلقه بنویس حله