String index out of range error help. Pig latin project - java

I'm doing an assignment that converts a string sentence into pig latin.
I've planned and written all the code but I get this error:
String index is out of range
Below is my code:
import java.util.*;
public class project1d {
public static void main(String[] args){
System.out.println("This is a pig latin translator. Enter a sentence to convert.");
Scanner console = new Scanner(System.in);
pigLat(console);
}
public static void pigLat(Scanner console){
String sentence = console.next();
int start = 0;
int end = 0;
int counter =0;
while(sentence.length()>0){
while(end<sentence.length()-1){
while(sentence.charAt(counter+1)!=' '){
counter++;
end = counter;
}
String word = sentence.substring(start,end);
int index= 0;
char letter= word.charAt(index);
while (letter != 'a' || letter != 'e' || letter != 'i' ||
letter != 'o' || letter != 'u'){
index++;
}
System.out.print(word.substring(index,word.length()-1)+"-");
System.out.print(word.substring(0,index-1)+"ay");
counter++;
start=end+1;
}
System.out.println("Do you wanna put in another? Press ENTER to quit");
sentence = console.next();
}
}
}
I think the third while loop is increasing one too many times, but I can't figure out how to fix this or if that's even the problem.
The actual java errors are listed below:
- java.lang.StringIndexOutOfBoundsException: String index out of range: 4
- at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
- at java.base/java.lang.String.charAt(String.java:693)
- at project1d.pigLat(project1d.java:15)
- at project1d.main(project1d.java:6)
Any help is appreciated.

Unfortunately, there are many things wrong with the code, aside of whether it does what it's intended to do or not.
You're increasing the indexes and checking the value without confirming whether those indexes are valid.
You are not clearing the variables before using them again after the first iteration of the whole thing.
You're increasing the index but you got an infinite loop at the letter check because you're not changing the letter when you increase the index.
The letter check is checking conditions that overlap - letter!= a || letter!=e will always be true, thus this makes it an infinite loop too.
As Andy pointed out: String sentence = console.next(); will only read a single word. Use nextLine instead.
Please try:
public static void pigLat(Scanner console){
String sentence = console.next();
int start = 0;
int end = 0;
int counter =0;
while(sentence.length()>0){
start = 0; end = 0; counter = 0;
while(end<sentence.length()-1){
while(sentence.length()>counter+1 && sentence.charAt(counter+1)!=' '){
counter++;
end = counter;
}
String word = sentence.substring(start,end);
if(word.length()>0){
int index = 0;
char letter = word.charAt(index);
while(index+1<word.lenght() && letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u'){
index++;
letter = word.charAt(index);
}
System.out.print(word.substring(index,word.length()-1)+"-");
System.out.print(word.substring(0,index-1)+"ay");
}
counter++;
start=end+1;
}
System.out.println("Do you wanna put in another? Press ENTER to quit");
sentence = console.nextLine();
}
It's just natural that your code has some mistakes, as you're still learning how to do that properly. Keep it up ;)

Related

Method to check if the string contains certain elements that accepts a parameter String

I am a student and kind of new to Java. For my homework I have to:
Ask the user to input a number (at least 7) using a do while loop.
Using a for loop I am required to ask the user to input that number of words.
Then I have to check if one of the words fulfills the given conditions:
The word must:
Start with an uppercase letter
End with a number
Contain the word "cse".
I am asked to create a method inside some code homework that does a specific task, the method should check all the required conditions, the name of the method should be countTest and it accepts the String as a parameter.
I will show you my code but I don't know how to create this specific method.
Output format
System.out.println("There as a total number of words " + count + " and
the ones that fulfill the condition are: " + condition);
The problem is, I dont know how to create the method or constructor or whatever it is called that calls all of the 3 methods inside it, and then connect that particular method to the main method!
I hope you guys can understand I am new to this, thank you in advance!
public class D6_6 {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Type a number that is at least 7");
int number = sc.nextInt();
int count = 0;
int condition = 0;
do{
if(number<7){
System.out.println("You should type a number that is at least 7 or higher");
number = sc.nextInt();
}
}
while(number<7);
sc.nextLine();
String str;
for(int i =0; i<number; i++){
System.out.println("Type a word");
str = sc.nextLine();
count++;
}
}
public boolean countTest(String str) {
}```
To check if the word start with an uppercase:
You can do that by first selecting the character you want to check by str.charAt(0). This will return a char that is the first letter of the input str.
To check if this char is an uppercase letter, you can easily use char.isUppercase(). This will return a boolean. You have to replace char by the name of the variable were you put the char of str.charAt(0) in.
To check if the last character is a number:
You can do that again by first selecting the last character by str.charAt(str.length()-1), were string.length-1 is the number of the last character.
To check if this character is a number, you can use the ascii table. Every character has it's own number. So if you want to check if your character is between 0 and 9, you can use char >= 48 || char <= 57 (look up in the ascii table). Again, char is the name of the variable were you put the char of str.charAt(str.length()-1) in.
To check if the word contains "cse":
There is a very easy method for that: str.contains("cse") will return a boolean that is true when "cse" is in the word and false when the word does not contain "cse".
I hope it is clear for you now!
I think I did it, thank you guys very much, I appreciate it!
public class D6_6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a number that is at least 7");
int number = sc.nextInt();
int count = 0;
int condition = 0;
do {
if (number < 7) {
System.out.println("You should type a number that is at least 7 or higher");
number = sc.nextInt();
}
}
while (number < 7);
sc.nextLine();
String str;
for (int i = 0; i < number; i++) {
System.out.println("Type a word");
str = sc.nextLine();
count++;
if((countTest(str))){
condition++;
}
}
if(count == 0){
System.out.println("No words typed");
} else {
System.out.println("Total number of words typed: " + count + ", which fulfill the condition: "+ condition);
}
}
public static boolean countTest(String str) {
return Character.isUpperCase(str.charAt(0)) && str.charAt(str.length() - 1) >= 48 || str.charAt(str.length() - 1) <= 57 || str.contains("cse");
}
}```

How to find the length of the last word in java?

I want to find the length of the last word to initially find the length of the second word in the phrase. here's my plan, If I find the length of the last phrase then in the for loop if I subtract x, it should give me the length of the second word always. This has to be done using for loop and a scanner, where the user inputs any phrase.
Basically, I need to know how to find the length of the last phrase. Thats what I need to know only.
here's my code:
else if (option == 2){
int counter = 0 ;
for (int x = 0; x < phrase.length() - x; x++){
//in the "int x = 0" i need to put the length of the last word
// in a phrase
char n = phrase.charAt(x);
if (n == x);
counter++;
}
System.out.print("Second word has "+counter+" letters");
}
With only the for-loop, length of last word in the phrase:
else if (option == 2){
int lastSpaceChar = 0;
for (int x = 0; x < phrase.length(); x++){
char n = phrase.charAt(x);
if (n == ' ') { //Indicate space
lastSpaceChar = x;
}
}
//When loop is finished, it will have the index of the last space
//So if you take total length - last space, you'll get the last word
int lengthOfLastWord = phrase.length() - spaceChar;
System.out.print("Last word has " + lengthOfLastWord + " letters");
}
If you want the second word, you need to do the same, except find 1st and 2nd space, because characters between first 2 spaces form the 2nd word.
else if (option == 2){
int firstSpace = -1;
int secondSpace = -1;
for (int x = 0; x < phrase.length(); x++){
char n = phrase.charAt(x);
if (n == ' ' && firstSpace == -1) { //Space found the first time
firstSpace = x;
} else if (n == ' ') {
//FirstSpace has already been found
//So this should be second
secondSpace = x;
break; //No need to look anymore, we have both spaces
}
}
//When loop is finished, you will have first 2 spaces
//Characters between first 2 spaces is 2nd word
int lengthOfSecondWord = firstSpace - secondSpace;
System.out.print("Second word has " + lengthOfSecondWord + " letters");
}
I'm assuming you're just learning, so this will do.
Of course, you should probably do error handling and look out for edge cases in actual practice.
The length of the last word in a sentence can be obtained like this:
public int getLengthOfLastWord(String sentence) {
if (sentence == null || sentence.length() == 0) {
return 0;
}
// Split the sentence into words on space(s).
String[] words = sentence.split("\\s+");
return words[words.length - 1].length();
}
To be accurate, you'll want to do more than just check for spaces between words, because if you are only looking for spaces then it is going to treat punctuation like commas and periods as part of the lengths of words. So your best bet is something like isAlphabetic().
If your plan is to loop through characters in the string (i.e. you don't want to use string functions), one very simple way (that wouldn't require you to change your code much) is to count forward through the array. The first time you find one where:
phrase.charAt(x).isAlphabetic()
is true, you know you've reached the beginning of the first "word" in the phrase.
Then you keep looking forward and when
!phrase.charAt(x).isAlphabetic()
Then you know you've reached the space between the first word and the second word. But there might be punctuation so you keep scanning forward until you're back to isAlphabetic() being true again, at which point you've reached the beginning of the second word. You now start counting characters until you find isAlphabetic() going false again (you've reached a space or punctuation). At which point you know you've reached the end of the second word, and now you can check your character count for how long it was.
This method doesn't count numbers, punctuation, spaces, and special characters as part of a phrase. So you might need to special-case it for things like an apostrophe in a contraction like "don't", but hopefully this gives you a general idea.
There are other approaches, but this is the closest to how you have your code right now. Obviously you can use String functions and get a shorter piece of code, etc, but you asked specifically for a version that conformed to your loop method.
int word_wanted = 2; // Want length of the second word. Or set to -1 if you want the last word.
int counter = 0; // How long is the current word
int on_word = 0; // What word number are we on
boolean in_word = false; // Are we currently in a word
for (int x = 0; x < phrase.length() - x; x++){
if (phrase.charAt(x).isAlphabetic()) {
if (!in_word) {
on_word++;
counter = 0;
in_word = true;
}
counter++;
} else {
if (in_word) {
in_word = false;
if (on_word == word_wanted) break; // Stop if we found the length of the word requested.
}
}
}
// counter now contains the length of the requested word. It can be returned
// as a return value from your method, or used for other purposes.
It is not clear from your question whether you want to find the length of the last word or the length of the second word from the start or the length of the second last word in a phrase. So, I've written solutions to all of these requirements. I've put enough comment in the code so that you can understand it easily.
Length of the last word:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter option: ");
int option = Integer.parseInt(keyboard.nextLine());
if (option == 2) {
System.out.print("Enter phrase: ");
String phrase = keyboard.nextLine();
int i;
// Start the loop from the end in the backward direction
for (i = phrase.length() - 1; i >= 0; i--) {
if (phrase.charAt(i) == ' ') {
System.out.println("Length of the last word: " + (phrase.length() - i - 1));
break;
}
}
// If the loop terminated without finding any space, it means there is only one
// word in the phrase
if (i == -1) {
System.out.println("Length of the last word: " + phrase.length());
}
}
}
}
A sample run:
Enter option: 2
Enter phrase: Good morning world
Length of the last word: 5
Another sample run:
Enter option: 2
Enter phrase: Good morning
Length of the last word: 7
Another sample run:
Enter option: 2
Enter phrase: Good
Length of the last word: 4
Length of the second word from the beginning:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter option: ");
int option = Integer.parseInt(keyboard.nextLine());
if (option == 2) {
System.out.print("Enter phrase: ");
String phrase = keyboard.nextLine();
int index = 0, counter = 0;
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == ' ') {
if (counter == 0) {
index = i;
}
counter++;
}
// Print the length when space has been found second time, and break the loop
if (counter == 2) {
System.out.println("Length of the second word: " + (i - index - 1));
break;
}
}
// If there are only two words in the phrase, space will be found only once. In
// that case the length of the second word will be phrase.length() - index - 1
if (counter == 1) {
System.out.println("Length of the second word: " + (phrase.length() - index - 1));
}
}
}
}
A sample run:
Enter option: 2
Enter phrase: Hello world
Length of the second word: 5
Another sample run:
Enter option: 2
Enter phrase: Good morning world
Length of the second word: 7
Length of the second last word:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter option: ");
int option = Integer.parseInt(keyboard.nextLine());
if (option == 2) {
System.out.print("Enter phrase: ");
String phrase = keyboard.nextLine();
int index = 0, counter = 0;
// Start the loop from the end in the backward direction
for (int i = phrase.length() - 1; i >= 0; i--) {
if (phrase.charAt(i) == ' ') {
if (counter == 0) {
index = i;
}
counter++;
}
// Print the length when space has been found second time, and break the loop
if (counter == 2) {
System.out.println("Length of second last word: " + (index - i - 1));
break;
}
}
// If there are only two words in the phrase, space will be found only once. In
// that case the length of the second last word (which is the first word out of
// the two words) will be index (as the loop is running in the backward
// direction)
if (counter == 1) {
System.out.println("Length of second last word: " + index);
}
}
}
}
A sample run:
Enter option: 2
Enter phrase: Good morning world
Length of second last word: 7
Another sample run:
Enter option: 2
Enter phrase: Hello world
Length of second last word: 5

charAt(0) Not returning first number but second

Piglatin translator. at the end I am trying to get the location of the first vowel. Index is set to be the location of every vowel, but with pig latin you only need the location of the first vowel. When I run the program I don't always get the location of the first vowel. it seems to give me the second number and not the first.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Assignment_4_Piglatin {
public static void main(String[] args) {
Scanner userWord = new Scanner(System.in);
System.out.println("K. Caleb Swallow");
System.out.println("Welcome to the Pig Latin Translator!");
boolean run = true;
while (run) {
System.out.println("Please enter a word(or press Q to quit):");
String firstLetter = "something";
String firstVowel = "test";
String word = userWord.next();
String vowels = "aeiou";
if (word.equals("Q")) {
System.exit(0);
}
firstLetter = Character.toString(word.charAt(0));
if (firstLetter.equals("a") || firstLetter.equals("e") || firstLetter.equals("i") || firstLetter.equals("o") || firstLetter.equals("u")) {
System.out.println(word + "way");
} else {
for (int index = 0; index < word.length(); index++) {
if (vowels.contains(String.valueOf(word.charAt(index)))) {
System.out.print(index);
String firstNumber = Integer.toString(index);
firstVowel = Character.toString(firstNumber.charAt(0));
}
}
}
System.out.println(firstVowel);
The example seems to have some redundant code in if..else condition. If you want to print the first vowels then you can do it with a simple for loop, e.g.:
String word = userWord.next().toLowerCase();
String vowels = "aeiou";
for(int i=0 ; i<word.length() ; i++){
if(vowels.contains(String.valueOf(word.charAt(i)))){
System.out.println(word.charAt(i));
break;
}
}
Please note that you need to do toLowerCase on the actual word in order for contains to work.
There are a few problems I can see off the bat, but the one that is likely causing this error is in these lines:
String firstNumber = Integer.toString(index);
firstVowel = Character.toString(firstNumber.charAt(0));
Think about what this is doing. First, you are making a String out of the index value, then you are saying that the first vowel is at the 0th index of that string.
Think of this example: hello
The program will run and assign "4" to firstNumber and firstVowel which isn't what you want.
However, if you only have one vowel, your program will "work".
What happens if you have over ten vowels? I know this isn't a realistic example, but say it happens. Your program will assign the index of the last vowel to firstNumber (say it's 15), then it will assign the first character of that to firstVowel (1). This doesn't make much sense at all, does it, especially if you don't have a vowel in index 1.
The main problem you are encountering for words less than 10 letters in length is that you are not just outputting the second number, you are outputting the last one. One way I like to deal with this is to go through the code and put in print statements where I'm not sure what a certain value is. For example, I'd put another print statement in your loop which tells you what letter you're looking at, like so:
System.out.println("LETTER: "+ String.valueOf(word.charAt(index)));
This will help you avoid confusion. The proper way to do this problem would be to use a break statement, such as in Darshan's answer. Alternatively, you could use the properties of the for loop:
firstVowel = "";
for (int index = 0; index < word.length() && firstVowel == ""; index++) {
CODE
}
Note that the second part of the for loop is a conditional statement. You already know that this can be used to cycle through the word's characters, but you can insert any logical statement there that you want. For this example, I set the default value of firstVowel to "" (setting it to null is a faux-pas, but that's another story). Then, each time the loop runs, it checks to see if the value of firstVowel has been changed, which will of course happen on the first time a vowel is run through the loop.
So in short, you need to modify the two lines at the beginning of my post and you need to find a way to break your loop when you find the first vowel. One solution has been given here, and another in Darshan Mehta's post.
public static void main(String[] args) {
Scanner userWord = new Scanner(System.in);
System.out.println("K. Caleb Swallow");
System.out.println("Welcome to the Pig Latin Translator!");
boolean run = true;
while (run) {
System.out.println("Please enter a word(or press Q to quit):");
String firstLetter = "something";
String firstVowel = "test";
String word = userWord.next();
ArrayList<Character> vowels = new ArrayList<>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
if (word.equals("Q")) {
System.exit(0);
}
firstLetter = Character.toString(word.charAt(0));
if (firstLetter.equals("a") || firstLetter.equals("e") || firstLetter.equals("i") || firstLetter.equals("o") || firstLetter.equals("u")) {
System.out.println(word + "way");
} else {
for (int index = 0; index < word.length(); index++) {
char indchar = word.charAt(index);
if (vowels.contains(word.charAt(index))) {
System.out.println(index);
firstVowel = Character.toString(word.charAt(index));
System.out.println(firstVowel);
index = word.length();
}
}
}
}
}
This is how I would do it. I changed the vowels String to an ArrayList so you can easily check if the char in the String word with the index is a vowel and the code works absolutely fine. It returns you the index where the first vowel is and what vowel it is.

Program to count vowels in several inputs of strings

What this programme does is;
Prompts for number of testcases,
User then inputs lines of strings which are the test case.
Program is to count the number of vowels in each test case.
then print out the each test case.
btw for this particular program "y" is also a vowel
For instance;
Number of test:
4
githsajklu
bsa uiqsacva
o h qi samsauq sajahhsa
skajayyosak
answer:
5 4 13 2
The problem is that the program doesnt read the last line/input. it just brings the count for the first 3 inputs but not the last. I hope I am clear enough
import java.util.Scanner;
/*
* Counts number of Vowels in each line
*/
public class VowelCount {
/*
*
*/
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
//prompt user for number of test case
System.out.println("Type in number of Test Cases");
int testcases = input.nextInt();
String[] line = new String[testcases];
String newline;
for(int i=0; i<testcases; i++)
{
//initialize input to line 1,2,3 e.t.c
line[i] = input2.nextLine();
//remove all white spaces
newline =line[i].replaceAll(" ", "");
display(testcases, newline);
}
}
/*
* counts how many vowels are in eace line of input
*/
public static int Counter(String input)
{
//start count from 0;
int counter = 0;
for(int i = 0; i<input.length(); i++)
{
//set character at position i to Dstr
char dStr = input.charAt(i);
//compare if dstr is a vowel
if(dStr == 'i' || dStr == 'u' || dStr == 'o' || dStr == 'a' || dStr == 'e' || dStr == 'y')
{
//increase when characte is a vowel
counter++;
}
}
//return the last count
return counter;
}
/*
* diplay the total count;
*/
public static void display(int testcases, String input)
{
System.out.print(" "+Counter(input));
}
}
Do a scan.nextLine() after you read the amount of test cases. Don't know why it works like that, someone feel free to explain, but if you're reading a an int, then a string, you can either do
int n = Integer.parseInt(scan.nextLine());
or
int n = scan.nextInt();
scan.nextLine();
Also, I know you didn't ask, but here's a much simpler way to count the amount of vowels.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.nextLine();
for(int i = 0; i < n; i++){
String str = scan.nextLine();
System.out.println(str.replaceAll("[^AEIOUaeiouYy]", "").length());
}
}
What that does is erase everything that is not a vowel (and y), and prints the length of it. Really not sure if it's faster, but it's a lot simpler.
Actually, here I'm testing and it is working just fine, it is getting exactly how many inputs I asked for.
My console:
Type in number of Test Cases
4
werty
2
sdfghj
0
xdcfvgh
0
xsdfgh
0
Sometimes, having less code can make things clearer:
void printVowelCount(String text) {
System.out.println(text.replaceAll("[^aeiouAEIOU]", "").length());
}

Palindrome tester with Java, ignoring spaces and punctuation

I have the program made up until the point where it has to ignore and punctuations and spaces in the thread and I was wondering if anyone could help me with the coding for that? What I've been trying out doesn't seem to be working. Here is what I have so far:
import java.util.Scanner;
public class PalindromeTester
{
public static void main (String[] args)
{
String str, another = "y";
int left, right;
char charLeft, charRight;
Scanner scan = new Scanner (System.in);
while (another.equalsIgnoreCase("y")) // allows y or Y
{
System.out.println ("Enter a potential palindrome: ");
str = scan.nextLine();
left = 0;
right = str.length() - 1;
while (left < right)
{
charLeft = str.charAt(left);
charRight = str.charAt(right);
if (charLeft == charRight)
{
left++;
right--;
}
else if (charLeft == ',' || charLeft == '.' ||
charLeft == '-' || charLeft == ':' ||
charLeft == ';' || charLeft == ' ')
left++;
else if (charRight == ',' || charRight == '.' ||
charRight == '-' || charRight == ':' ||
charRight == ';' || charRight == ' ')
right--;
else
break;
}
System.out.println();
if (left < right)
System.out.println ("That string is NOT a palindrome.");
else
System.out.println ("That string IS a palindrome.");
System.out.println();
System.out.print ("Test another palindrome (y/n)? ");
another = scan.nextLine();
}
}
}
Just to clarify what Jim Garrison said, the regex you need is the following
String m = "Madam, I'm'',.,.'' Adam";
m = m.toLowerCase().replaceAll("\\W", "");
This will leave only letters and digits and remove whitespace and punctuation, i.e. m will become "madamimadam" and you can run you regular palindrome test on that string.
You can learn more about regular expressions here
This looks like a really old post but I think I stumbled upon a simpler solution for a palindrome test. This checks the first and last characters and moves inwards and exits the program as soon as the characters do not match.
public class CharTest {
public static void main(String[] args) {
//converts string to lowercase and replaces everything except numbers
// and alphabets
String s = "Niagara. O roar again!".toLowerCase().replaceAll("\\W", "");
int j=0;
int k = s.length() - 1;
while(j < s.length() / 2) { //loops until half the length of the string if
//even and floor value if odd.
if (s.charAt(j++) != s.charAt(k--)){//check for first and last chars
//and go inwards. if char do not match print 'Not a Palindrome' and exit
System.out.println("Not a Palindrome");
System.exit(0);}
}
System.out.println("Palindrome"); //if every chars match print "Palindrome"
}
}
This code for determine if a word is a palindrome can be much more simplified. Find updated Code
String word;
int z;
int y = 0;
int i = 0;
char letter;
Scanner input = new Scanner(System.in);
System.out.print("Enter a word: ");
word = input.nextLine();
word = word.replaceAll("\\s+", "");
word = word.toLowerCase();
z = word.length()-1;
while (i <= z){
if ((letter = word.charAt(i)) == (letter = word.charAt(z-i))){
y += 1;
}
i += 1;
}
if (y == (z+1)){
System.out.println("The word IS a palindrome");
}
else{
System.out.println("The word is NOT a palindrome");
}
}
}
You could simplify the code significantly by removing all the spaces and punctuation before you start. Look at String.replaceAll(regex,replacement). You would write a regular expression to match blanks and punctuation, and provide an empty string ("") as the replacement. This will return a new string containing the original minus the characters you want to ignore.
This is the programming assignment from the Java Software Solutions (PP3.11) that I assign my students. Ironically the teacher solution uses Character.isLetterOrDigit(___) (which is never mentioned in the book) and uses methods to get rid of spaces and punctuation (having not even taught methods at that point in the book), and char is not an official part of the AP CS subset. Silly publishers.
Look at char's documentation entry. Specifically the isLetterOrDigit method. If that method returns false, then it's punctuation or a space. There are other methods in there as well that can help with that.
Your Problem: You are not ignoring the case of the letters. So if you try Able was I, ere I saw Elba, it will not come back correctly, although it is a true palindrome.

Categories

Resources