Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Taking punctuation marks and spaces out've a string

Posted on 2008-10-10
1
Medium Priority
?
1,064 Views
Last Modified: 2012-05-05
I am writing some code where i have to reverse a word, place only alphabetical characters onto the stack and the queue, and convert all uppercase letters to lowercase and check to see if its a palindrome and im supposed to use a Queue and a Stack.


So my question is, how do i implement sometime of checking to make sure i only put alphabetical characters onto the stack?

The input file im using will only have spaces and punctuation marks.
import java.util.*;
import java.io.*;
import java.lang.*;
 
class LetterStack
	{
	private int maxSize;
	private char[] stackArray;
	private int top;
	
	public LetterStack(int max)
		{
		maxSize = max;
		stackArray = new char[maxSize];
		top = -1;
		}
	public void push(char j)
		{
		stackArray[++top] = j;
		}
	public char pop()
		{
		return stackArray[top--];
		}
	public char peek()
		{
		return stackArray[top];
		}
	public boolean isEmpty()
		{
		return (top == -1);
		}
	}
class Reverse
	{
	private String input;
	private String output;
	
	public Reverse(String in)
	{
	input = in;
	}
	public String doRev()
	{
	int stackSize = input.length();
	LetterStack tehStack = new LetterStack(stackSize);
	for(int j=0; j<input.length(); j++)
		{
		char ch = input.charAt(j);
		tehStack.push(ch);
		}
	output = "";
	while( !tehStack.isEmpty() )
		{
		char ch = tehStack.pop();
		output = output + ch;
		}
	return output;
	}
}
 
public class PalindromeApp{
	public static void main (String[] args) throws IOException{
		String input,output;
		Scanner inFile = new Scanner(new FileReader(args[0]));
		PrintWriter outFile = new PrintWriter(args[1]);
		while(inFile.hasNext())
			{
			input = inFile.nextLine();
			Reverse  tehReverse = new Reverse(input);
			output = tehReverse.doRev();
			if(input.equals(output))
				outFile.println("PALDINDROME");
			else
				outFile.println("NOT PALINDROME");
			}
		outFile.close();
		}
		
	}

Open in new window

0
Comment
Question by:austinemser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
gcauthon earned 2000 total points
ID: 22690636
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') tehStack.push(ch);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question