The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 4 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I have a palindrome Assignment for my java class and was stuck on one part of the Assignment. So the pat where I am stuck is asking for me to determine the type of palindrome, that is I have to determine if the palindrome is a word, phrase, or number.
Â
I have uploaded the assignment guidelines in the attachment as well as my code.
import java.util.*;
public class Assign1 {
   static Scanner in = new Scanner(System.in);
   public static void main (String [] args) {
      String line = getInputLine();
      while (!isEmptyLine (line)) {
         if (isPalindrome (line))
            System.out.println ("\"" + line +
                  "\" is a palindrome and a " + getPalType (line));
         else
            System.out.println ("\"" + line + "\" is not a palindrome");
         line = getInputLine();
      }
      System.out.println ("End of program");
   }
   private static String getInputLine () {
      System.out.print("Enter a line of input: ");
      String inputString = in.nextLine();
      return inputString;
   }
   private static boolean isEmptyLine (String str) {
      boolean ifEmpty = false;
      if(str == null) {
         ifEmpty = true;
         return ifEmpty;
      }
      else
         return ifEmpty;
   }
   private static boolean isPalindrome (String str) {
      int left = 0;
      int right = str.length()-1;
      boolean okay = true;
      while(okay && left < right) {
         char ch1 = str.charAt(left);
         if((!Character.isDigit(ch1) && !Character.isAlphabetic(ch1))) {
            left++ ;
         }
         else {
            char ch2 = str.charAt(right);
            if ((!Character.isDigit(ch2) && !Character.isAlphabetic(ch2))) {
               right-- ;
            }
            else
            {
               ch1 = Character.toUpperCase(ch1);
               ch2 = Character.toUpperCase(ch2);
               if (ch1 == ch2) {
                  left++ ;
                  right-- ;
               }
               else
               {
                  okay = false;
               }
            }
         }
      }
      return okay;
   }
   private static String getPalType (String str) {
      int count = 0;
      String palType = "";
      for(int index = 0; index <= str.length()-1; index ++) {
         if(str.charAt(index) < ':' && str.charAt(index) > ' ' )
        Â
            if(str.charAt(index) == ' ')
               palType = "Phrase";
            count++;
      }
      if (palType == "Phrase")
         return palType ;
      if (count == str.length())
         return "Number" ;
      else
         return "Word" ;
   }
}
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly