I'm trying to make a Palindrome program and this loop is giving me trouble:
for (int i= 0; i < 79; i++)
{
character[i] = scan.next();
if (character[i] == ".")
{break;
}
}
The user enters a word, and when they are done, they enter a period, which is supposed to break them out of the loop. But when I run the program and enter a period, nothing happens. It just allows me to enter letters until the array is full. Why is the program skipping over the if-statement?
Here is the full program (And yes, I'm sure there are plenty of other mistakes, but I'll try to fix them on my own later. Forgive me, I'm new to Java):
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
System.out.println("Please enter a word or phrase. Enter a '.' when you are done.");
Scanner scan = new Scanner (System.in);
String[] character = new String[79];
for (int i= 0; i < 79; i++)
{
character[i] = scan.next();
if (character[i] == ".")
{break;
}
}
if (isPalindrome(character)==true)
{System.out.println("It is a palindrome.");
}
if (isPalindrome(character) != true)
{System.out.println("It is not a palindrome.");
}
}
public static Boolean isPalindrome (String[] chars){
String[] reverse = new String[79];
for (int i=reverse.length-1;i>=0; i--)
{
if(reverse[i] == chars[i])
{return true;
}
else
return false;
}
}
}