Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Java] Ignore spaces and punctuations
New Posts  All Forums:Forum Nav:

[Java] Ignore spaces and punctuations

post #1 of 4
Thread Starter 
I need to make this palindrome program ignore spaces and punctuations.
Code:
import java.util.Scanner;

        public class Palindrome {

        public static void main (String[] args){
        String str, another = "y";
        int left, right;
        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 (str.charAt(left) == str.charAt(right) && left < right)
        {
        left++;
        right--;
        }
        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();
        }
        }
        }
post #2 of 4
Use a regular expression to find any non-letter character and just replace them with an empty string. Then you'll just be left with a-z and A-Z. Something like this:
Code:
str.replaceAll("[^a-zA-Z]", "")
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #3 of 4
Quote:
Originally Posted by Capt View Post

I need to make this palindrome program ignore spaces and punctuations.
Code:
import java.util.Scanner;

        public class Palindrome {

        public static void main (String[] args){
        String str, another = "y";
        int left, right;
        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 (str.charAt(left) == str.charAt(right) && left < right)
        {
        left++;
        right--;
        }
        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();
        }
        }
        }

Looks very similar to one of the programs we did in my Java 2 class a few weeks back. May I ask which book this is from?

§
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
post #4 of 4
Thread Starter 
Quote:
Originally Posted by shlunky View Post

Looks very similar to one of the programs we did in my Java 2 class a few weeks back. May I ask which book this is from?

§

Java Software Solutions

Thanks randomizer. +rep
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [Java] Ignore spaces and punctuations