5 Replies - 1905 Views - Last Post: 13 November 2011 - 11:00 PM Rate Topic: -----

#1 kokolet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-November 11

Palindrome using methods

Posted 13 November 2011 - 05:54 AM

I am having problems in getting the commands for the "system.out.println" to work. Althought the program runs, it doesnt show the command.

import java.util.*; 

public class KOKOLET{  
  public static void main(String [] args){
    
    
    Scanner keyboard = new Scanner(System.in);
    boolean isRunning = true;
    boolean isDigit = true;
    boolean isLetter = true;
    
    
    while (isRunning) {    
      System.out.println("Please give a word or phrase for the palindrome test: ");
      String input = keyboard.nextLine();
      String inputUpperCase = input.toUpperCase();
      input= input.toLowerCase();
      
      if (input.equals("")) { // quit when no input is given (empty string)
        isRunning = false;      
      }
    }
  }
  
  /**...................................................isDigit
    *PURPOSE: determine if its a digit character
    *PARAMETER: none
    *RESULT: string output System.out
    **/  
  
  public static boolean isDigit(char possibleDigit){     
    if ( possibleDigit >= '0' &&  possibleDigit <= '9'); // is a digit      
    return true;
    
  }
  
  /**...................................................isLetter
    *PURPOSE: determine if its a character
    *PARAMETER: none
    *RESULT: string output System.out
    **/  
  
  public static boolean isLetter(char possibleLetter){     
    if (possibleLetter >= 'A' && possibleLetter <= 'Z'); // is a letter      
    return true;
  }
  
  /**...................................................filterInput
    *PURPOSE: accept a String that may contain non-alphanumeric characters
    *PARAMETER: none
    *RESULT: string output System.out
    **/  
  public static String filterInput(String input){     
    input= input.toLowerCase();
    String inputUpperCase = input.toUpperCase();   
    String filterInput = "";
    //char possibleDigit;
    int index = 0; // start at index 0 and work our way across the string
    
    //if (Character.isDigit(input.charAt(possibleDigit))){
    
    while (index < inputUpperCase.length()) {
      
      char possibleLetter= inputUpperCase.charAt(index); // use uppercase to simplify upper/lower testing
      char nextCharacter= inputUpperCase.charAt(index); // use uppercase to simplify upper/lower testing
      
      if ( (possibleLetter >= 'A' && possibleLetter <= 'Z') ||  // is a character, OR
          (nextCharacter >= '0' && nextCharacter <= '9') ) { // is a digit
        filterInput += Character.toUpperCase(inputUpperCase.charAt(index)); // append to filtered version. otherwise, don't append
      }
      index++; // move to next character of input
    } 
    
    return input;
  }    
  
  /**...................................................isPalindrome
    *PURPOSE:  determine if a specified String is a palindrome
    *PARAMETER: none
    *RESULT: string output System.out
    **/ 
  public static boolean isPalindrome(String palindrome){    
    boolean isPalindrome = true; // assume that it is
    int index = 0;
    String filterInput = "";
    
    while (index < filterInput.length()/2 && isPalindrome) { // only need to check half way across
      int mirrorIndex = (filterInput.length()-1) - index; // corresponding character from end of the string.
      
      if (filterInput.charAt(index) != filterInput.charAt(mirrorIndex)) // for palindromes, the index and mirror index should be equal
        isPalindrome = false; // it wasnt equal in this case, so it cannot be a palindrome 
      
      index++;   
    }
    return true;
  }
  
  
  /**...................................................isPalindromePhrase
    *PURPOSE: whether or not the palindrome is a phrase.
    *PARAMETER: none
    *RESULT: string output System.out
    **/
  public static boolean isPalindromePhrase(String palindrome){
    boolean isPalindromePhrase = true;
    int longestPalindromeLength = 0;
    String longestPalindrome = "";  
    int totalPalindromeWords=0;
    int totalPalindromePhrases=0;
    int cumulitativeLength = 0; 
    String filterInput = "";  
    boolean isPalindrome = true;  
    
    Scanner keyboard = new Scanner(System.in);
    String input = keyboard.nextLine();
    
    boolean isWord = (input.indexOf(" ") == -1);
    if(isPalindrome) {
      
      // test if our new palindrome is longer than our recorded "longest"
      if (filterInput.length() > longestPalindromeLength) {
        // updte "longest" record
        longestPalindromeLength = filterInput.length();
        longestPalindrome = input; // SAVE unfiltered input for nice-looking output
      }
      
      cumulitativeLength+=filterInput.length();
      
      System.out.print("\""+input+"\" is a palindrome ");
      
      if (isWord) {
        totalPalindromeWords++;
        System.out.println("word.");
      } else { // is phrase
        totalPalindromePhrases++;
        System.out.println("phrase.");
      }
      
    } return true;
  }
  
  
  /**...................................................roundTo
    *PURPOSE: round a floating-point number to some specified number 
    of decimal places
    *PARAMETER: none
    *RESULT: string output System.out
    **/
  public static double roundTo(double number, int decimalPlaces){
    int totalTests=0;
    int totalPalindromeWords=0;
    int totalPalindromePhrases=0;
    double percentPalindromeWords;
    double percentPalindromePhrases;
    double averagePalindromeLength;
    int cumulitativeLength = 0;
    
// calculate statistics
    percentPalindromeWords = (double)totalPalindromeWords / totalTests * 100;
    percentPalindromePhrases = (double)totalPalindromePhrases / totalTests * 100;
    averagePalindromeLength = (double)cumulitativeLength/(totalPalindromeWords+totalPalindromePhrases);
    
    // scale all to 2 decimal places
    percentPalindromeWords = Math.round(percentPalindromeWords*100)/100.0;
    percentPalindromePhrases = Math.round(percentPalindromePhrases*100)/100.0;
    averagePalindromeLength = Math.round(averagePalindromeLength*100)/100.0;
    
    return number;
  }
  
  
  /**...................................................printStatistics
    *PURPOSE: contain all of the code to calculate and print the 
    final statistics 
    of decimal places
    *PARAMETER: none
    *RESULT: string output System.out
    **/
  public static void printStatistics(int totalInputs, int totalPalindromes, 
                                     int words, int phrases, String longestPalindrome, int longestPalindromeLength, int
                                       averagePalindromeLength, double percentPalindromePhrases, double percentPalindromeWords){
    int totalPalindromeWords=0;
    int totalPalindromePhrases=0;  
    
    
// output statistics
    System.out.println("Stats:");
    System.out.println("\t"+ percentPalindromeWords+"% were palindrome words.");
    System.out.println("\t"+ percentPalindromePhrases+"% were palindrome phrases.");
    System.out.println("\tThe longest palindrome was \""+longestPalindrome+"\" at "+longestPalindromeLength+" alphanumeric characters.");
    System.out.println("\tThe average palindrome length was "+ averagePalindromeLength +" alphanumeric characters.");
    
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome using methods

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Palindrome using methods

Posted 13 November 2011 - 08:04 AM

If you are on about the first println -

 System.out.println("Please give a word or phrase for the palindrome test: ");


it works fine for me..
Was This Post Helpful? 0
  • +
  • -

#3 kokolet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-November 11

Re: Palindrome using methods

Posted 13 November 2011 - 01:45 PM

notthe firstone,the other ones
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Palindrome using methods

Posted 13 November 2011 - 02:05 PM

Well you never actually call the methods that contain the other printlns anywhere in your main method. Therefore it is no supprise that they don't show up.
Was This Post Helpful? 0
  • +
  • -

#5 kokolet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-November 11

Re: Palindrome using methods

Posted 13 November 2011 - 03:53 PM

View PostRyano121, on 13 November 2011 - 09:05 PM, said:

Well you never actually call the methods that contain the other printlns anywhere in your main method. Therefore it is no supprise that they don't show up.



Thayt is what i am having problems with. When I call the methods i get errors. Could you please help me with that?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Palindrome using methods

Posted 13 November 2011 - 11:00 PM

Please:
-Post your revised code
-Post your specific problems or errors exactly as they appear from your compiler

Without these, we are the blind leading the blind.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1