Can someone help me figue out why I keep getting an erorr that I am missing a return statement.

import java.util.*;

public class ec1
{
	static Scanner kb = new Scanner(System.in);

	public static void main(String[] args)
	{
		String word = " ";
		String s = " ";
		char letter;
		int i=0, j=0;
		boolean isPal;

	  	System.out.println("Enter a string of text to check if a palindrome: (end to file");
	  	while (kb.hasNext() )
	  	{
			s =kb.nextLine();

		 	 for (i=0; i < s.length(); i++) {
			 letter = s.charAt(i);
			 if(Character.isLetter(letter))
			 {
				 word += letter;
		 	 }

	 	}
			if(isPal(word))
			System.out.println(word + " is not a Palindrome");
			else
			System.out.println(word + " is a Palindrome");
			word = "";
			System.out.println("Enter a string to check if a palindrome: (end to file");
			}
	}
    public static boolean isPal(String s)
	{
		int len = s.length();
		int i, j;
		char ch1, ch;

		j = len - 1;

		for (i = 0; i <= (len -1)/2; i++)
		{
			ch = s.charAt(i);
			ch1 = s.charAt(j);
			if(!Character.isLetter(ch) && ! Character.isLetter(ch1) && ch != ch1)
				//j--;
				{
				return true;
			    }
			    else
			    {
			    return false;
			    }
		}
	 }
}

Recommended Answers

All 4 Replies

because you are missing a return statement.
if, in such case, that (for instance) len = 0
where is your return statement?

you'll need to place a return outside of the for-loop, just to make sure that one will be reached if some unexpected input is given.

because you are missing a return statement.
if, in such case, that (for instance) len = 0
where is your return statement?

you'll need to place a return outside of the for-loop, just to make sure that one will be reached if some unexpected input is given.

Thank you for your help. I think this is what you mean. However now the error is: class, interface or num expected.

public static boolean isPal(String s)
	{
		int len = s.length();
		int i, j;
		char ch1, ch;

		j = len - 1;

		for (i = 0; i <= (len -1)/2; i++)
		{
			ch = s.charAt(i);
			ch1 = s.charAt(j);
			if(!Character.isLetter(ch) && ! Character.isLetter(ch1) && ch != ch1)
				//j--;
				{
				return true;
			    }
			    else
			    return false;
			    System.out.println(ch + " is not a Palindrome");
		 	    }
	 	}
 	}
}

nope, that's not what I ment.
you still have all your return statements within that for loop, so you still have the same problem.
now you've added the problem you're mentioning, by placing a print-statement after a return statement.

what I ment was something like:

public static boolean isPalindrome(String s){
  boolean returnVal = true; // 
  for ( int i = 0; i < s.length(); i++){
     // check s.charAt(i) && s.charAt(s.length() - i) for equality
     // if not equal
     returnVal = false;
  }
  return returnVal;
}

this way, you can add all the checks you need, without being afraid not to have a return statement present

nope, that's not what I ment.
you still have all your return statements within that for loop, so you still have the same problem.
now you've added the problem you're mentioning, by placing a print-statement after a return statement.

what I ment was something like:

public static boolean isPalindrome(String s){
  boolean returnVal = true; // 
  for ( int i = 0; i < s.length(); i++){
     // check s.charAt(i) && s.charAt(s.length() - i) for equality
     // if not equal
     returnVal = false;
  }
  return returnVal;
}

this way, you can add all the checks you need, without being afraid not to have a return statement present

Ok thnak you Stultuske. I will keep coding away.

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.