Solved

Taking punctuation marks and spaces out've a string

Posted on 2008-10-10
1
1,056 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
1 Comment
 
LVL 3

Accepted Solution

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now