Ive been working on a project in eclipse. I will post pictures for the problem a
ID: 670997 • Letter: I
Question
Ive been working on a project in eclipse. I will post pictures for the problem after this and provide the code I have so far. Eclipse wont run this code because it is giving an error since the scanner is never closed. But I am still new to java and not sure how to fix it.
import java.util.*;
public class Palindrome{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String palindrome = in.nextLine();
char[] chars = palindrome.toLowerCase().toCharArray();
isPalindrome(chars,palindrome.length());
}
private static boolean isPalindrome(char[] a,int used) {
int i = 0,j = used;
while (i < j) {
if (Character.isWhitespace(a[i])) {
i++;
continue;
}
if (Character.isWhitespace(a[j])) {
j--;
continue;
}
if (Character.toLowerCase(a[i]) != Character.toLowerCase(a[j])) {
return false;
} else {
i++;
j--;
}
}
return true;
}
}
salindrome is a word or phrase that reads the same forward backward, ignoring blanks and considering uppercase and versions letter of the same to be equalthe and lowercase palindromes: For example, following are warts n straw a radar • Able was Iere I saw Elba • Xyzczyx write a program that will accept a sequence of characters terminated period and will decide whether the string without the period is a a by palindrome. You may assume that the input contains only and letters blanks and is at most 80 characters long, Include a loop that allows the user to check additional strings until she or he requests that the program end. (Hint: Define a static method called isPalindrome that begins as follows /* * Precondition: The array a contains letters and blanks in positions alo ] through a[used - 1). Returns true if the string is a palindrome and false otherwise, public static boo lean isPalindrome (eharu a int used)Explanation / Answer
package Polindrom;
import java.util.Scanner;
public class main {
publicstatic void main(String[] args) {
char[]symb = new char[80];
Scannercs = new Scanner(System.in);
inti = 0;
intch;
while((ch = cs.nextLine().toCharArray()[0]) != 46)// reads from console
{
symb[i]= (char) ch;
i++;
}
System.out.println(isPalindrome(symb,i));
}
publicstatic boolean isPalindrome(char[] a, int used) {
char[]tmp = new char[a.length];
intind = 0;
for(int i = 0; i < used - 1; i++) {
if(a[i] != 32) {
tmp[ind]= a[i];
ind++;
}
}
intn = (ind) / 2;
Strings1 = "";
Strings2 = "";
for(int i = 0; i < n; i++) {
s1+= a[i];
s2+= a[used - 1 - i];
if(!s1.equalsIgnoreCase(s2)) {
returnfalse;
}
}
returntrue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.