Question:  A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. Ex: If the input is: bob the output is: palindrome: bob Ex: If the input is: bobby the output is: not a palindrome: bobby Hint: Start by just handling single-word input, and submit for grading. Once passing single-word test cases, extend the program to handle phrases. If the input is a phrase, remove or ignore spaces.   My Issue: I'm unable to use replaceAll(" ", "") as the java program wouldn't allow me as we're working on loops this week and haven't touch upon it. I've attached a screenshot where replaceAll method resulted in no output with the error displayed "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16". Regardless,I don't know how to include the space in the string variable to include the entire sentence as a palindrone.  My code:  import java.util.Scanner; public class LabProgram { public static void main(String[] args) { /* Type your code here. */String word=""; String opposite = ""; Scanner scnr= new Scanner(System.in); word = scnr.nextLine(); for(int i = word.length() - 1; i >= 0; i--){ opposite = opposite + word.charAt(i); } if(word.equals(opposite)){ System.out.println(“palindrome: " +word); } else{ System.out.println(“not a palindrome: "+word); } } } My initial code with replaceAll:  String word = "";      String oppos = "";         Scanner scnr = new Scanner(System.in);         word = scnr.nextLine();         String str = word.replaceAll(" ","");                  for(int i = word.length() - 1; i >= 0; i--){             oppos = oppos + str.charAt(i);         }         if(str.equals(oppos)){             System.out.println(“palindrome: " +word);         }         else{             System.out.println(“not a palindrome: "+word);         }     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question
100%

Question: 

A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.

Ex: If the input is:

bob

the output is:

palindrome: bob

Ex: If the input is:

bobby

the output is:

not a palindrome: bobby

Hint: Start by just handling single-word input, and submit for grading. Once passing single-word test cases, extend the program to handle phrases. If the input is a phrase, remove or ignore spaces.

 

My Issue:

I'm unable to use replaceAll(" ", "") as the java program wouldn't allow me as we're working on loops this week and haven't touch upon it. I've attached a screenshot where replaceAll method resulted in no output with the error displayed "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16". Regardless,I don't know how to include the space in the string variable to include the entire sentence as a palindrone. 

My code: 

import java.util.Scanner;

public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */String word="";
String opposite = "";
Scanner scnr= new Scanner(System.in);
word = scnr.nextLine();


for(int i = word.length() - 1; i >= 0; i--){
opposite = opposite + word.charAt(i);
}
if(word.equals(opposite)){
System.out.println(“palindrome: " +word);
}

else{
System.out.println(“not a palindrome: "+word);
}
}
}

My initial code with replaceAll: 

String word = "";
     String oppos = "";
        Scanner scnr = new Scanner(System.in);
        word = scnr.nextLine();
        String str = word.replaceAll(" ","");
        
        for(int i = word.length() - 1; i >= 0; i--){
            oppos = oppos + str.charAt(i);
        }
        if(str.equals(oppos)){
            System.out.println(“palindrome: " +word);
        }

        else{
            System.out.println(“not a palindrome: "+word);
        }
    }
}

1: Compare output ^
1/1
Input
bob
Your output
palindrome: bob
2: Compare output a
1/1
Input
sees
Your output
palindrome: sees
3: Compare output ^
0/1
Output differs. See highlights below.
Special character legend
Input
never odd or even
Your output
not a palindrome: never odd or even
Expected output
palindrome: never odd or even
Transcribed Image Text:1: Compare output ^ 1/1 Input bob Your output palindrome: bob 2: Compare output a 1/1 Input sees Your output palindrome: sees 3: Compare output ^ 0/1 Output differs. See highlights below. Special character legend Input never odd or even Your output not a palindrome: never odd or even Expected output palindrome: never odd or even
1: Compare output a
1/1
Input
bob
Your output
palindrome: bob
2: Compare output a
1/1
Input
sees
Your output
palindrome: sees
3: Compare output a
0/1
Exception in thread "main" java.lang.StringIndexOutofBoundsException: String index out of
at java.base/java.lang.StringLatin1.charAt (StringLatinl.java:47)
at java.base/java.lang.String.charAt (String.java: 693)
at LabProgram.main (LabProgram.java:12)
Input
never odd or even
Your output Your program produced no output
Expected output
palindrome: never odd or even
4: Compare output a
1/1
Transcribed Image Text:1: Compare output a 1/1 Input bob Your output palindrome: bob 2: Compare output a 1/1 Input sees Your output palindrome: sees 3: Compare output a 0/1 Exception in thread "main" java.lang.StringIndexOutofBoundsException: String index out of at java.base/java.lang.StringLatin1.charAt (StringLatinl.java:47) at java.base/java.lang.String.charAt (String.java: 693) at LabProgram.main (LabProgram.java:12) Input never odd or even Your output Your program produced no output Expected output palindrome: never odd or even 4: Compare output a 1/1
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education