Hi i have written a program to reconise palendromes in a sentance. I have written a method to reverse a word but dont know how to get the string tokeniser to reconise the words in a sentance. could any one give me some advice on how to use HasMoreTokens
this is the code i have written so far

//filename Palindrome.java
//created Monday 15th nov 2004
// Finds Palindromes in a sentance

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Palindrome
{
	public static void main(final String[] pArgs) throws IOException
	{

		final InputStreamReader tInputStreamReader = new InputStreamReader(System.in);
		final BufferedReader tKeyboard = new BufferedReader(tInputStreamReader);

	// get user to input a sentance
		System.out.println(" A Palindrome is a word that is spelt the same way backwards i.e bob. dad, abba");
		System.out.println();
		System.out.println(" Please Type in a Sentance with some Palindromes in it ");
		System.out.flush();
		final String tLine1 = tKeyboard.readLine();


	// takes words and splits them into separate strings
		final StringTokenizer tTokens 		= new StringTokenizer (tLine1, " ");
		final String tWord1 			= tTokens.nextToken();

		while ( tTokens.hasMoreTokens() )
		{
 			final String tWord = tTokens.nextToken();
		}

	}


	public static boolean iPalindrome (final String pWord1)
	{
		final int tStringLength = pWord1.length();
		for (int tCharNumber = tStringLength -1;  tCharNumber >=0; tCharNumber--)
		{
			System.out.print(pWord1.charAt(tCharNumber));
		}

		return true;

		//System.out.println();
	}
	// end method
}

any help and advice would be appeciated i have to submit my course work in two weeks and have an exam on this stuff

Dani AI

Generated

Good start, — the overall approach is fine. Two concrete problems to fix: you call nextToken() once before the loop and never process that first token, and your palindrome routine only prints the reversed word and returns true unconditionally, so nothing gets tested. Also new StringTokenizer(line, " ") uses a single-space delimiter (it will leave punctuation attached and won't treat tabs/multiple spaces as expected). As pointed out, hasMoreTokens() returns a boolean and is the correct condition for a while loop — the issue is how tokens are consumed and how each token is checked.

A compact, practical pattern to follow (tokenize, sanitize, then test) looks like this:

StringTokenizer st = new StringTokenizer(line); // default: any whitespace
while (st.hasMoreTokens()) {
    String raw = st.nextToken();                // get next token
    String word = raw.replaceAll("[^A-Za-z]", "") // remove punctuation
                     .toLowerCase();
    if (word.length() == 0) continue;
    if (isPalindrome(word)) {
        System.out.println(raw + " is a palindrome");
    }
}

public static boolean isPalindrome(String s) {
    return s.equals(new StringBuilder(s).reverse().toString());
}

Notes and troubleshooting tips:

  • Remove punctuation and compare in one case (use replaceAll("[^A-Za-z]", "") and toLowerCase()), otherwise words like "Anna," will fail.
  • Either use StringTokenizer(line) (no delimiter) or line.split("\\s+") / Scanner instead of " " so multiple whitespace types are handled.
  • If the first word disappears, check for an extra nextToken() call before your loop — that consumes the token.
  • Add temporary System.out.println(raw) inside the loop to verify what you’re processing while debugging.

This fixes the token consumption bug, makes the palindrome check meaningful, and handles punctuation and case-insensitivity.

When you use .hasMoreTokens(), it returns a value and it seem to be that you aren't doing anything with this value - "while ( tTokens.hasMoreTokens() )" - so the while() never knows what is being satisfied or not. You need to make a condition in a while statement. Look up what kind of value the .hasMoreTokens() returns and you should be able to fix this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.