Reputation: 71
import java.util.Scanner;
public class WordShuffle
{
static Scanner in = new Scanner(System.in);
private static String getText()
{
log("in getText");
String t;
log("Enter only alphabets");
t = in.next();
log("returned t= " + t);
return t;
}
private static void shuffleText(final String txt)
{
log("in shuffle= " + txt);
final int textLenght = txt.length();
final char textArray[] = new char[textLenght];
for (int i = 0; i < textLenght; i++)
{
textArray[i] = txt.charAt(i);
}
for (int i = 0; i < textLenght; i++)
{
}
}
public static void log(final String l)
{
System.out.println(l);
}
private static String validateText()
{
final String text = getText();
log("in validate= " + text + "");
final char[] tc = text.toCharArray();
for (final char t : tc)
{
if (Character.isLetter(t))
{
log("character = " + t);
}
else
{
System.out.println("Error occured, non alphabet found in text");
log("error = " + t);
validateText();
}
}
log("validate returned " + text);
return text;
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args)
{
// TODO code application logic here
shuffleText(validateText());
}
}
if this is run with sample texts "abc", it runs fine, with smaple text "abc3" it points out the error 3 and recurs the validate() method on requesting for input again, if sample text "abc" is entered it return the text "abc" and "abc3" one after the other.
console output is below
run:
in getText
Enter only alphabets
abc3
returned t= abc3
in validate= abc3
character = a
character = b
character = c
Error occured, non alphabet found in text
error = 3
in getText
Enter only alphabets
abc
returned t= abc
in validate= abc
character = a
character = b
character = c
validate returned abc
validate returned abc3
in shuffle= abc3
BUILD SUCCESSFUL (total time: 8 seconds)
Upvotes: 1
Views: 1240
Reputation: 71
I did as suggested, here's the new validateText() method
private static String validateText() {
String text = getText();
log("in validate= " + text + "");
char[] tc = text.toCharArray();
for (char t : tc) {
if (Character.isLetter(t)) {
log("character = " + t);
}
else {
System.out.println("Error occured, non alphabet found in text");
log("error = " + t);
log("validate returned " + text);
return validateText();
}
}
return text;
}
i returned validateText() in else an had to return final correct text.
Upvotes: 2
Reputation: 283
The answers above are correct. Also recursively calling the function more and more times may lead to an overflow so perhaps you should rethink your design. Why don't you use a simple regex instead? Google: regular expressions, regular expressions java It's what everyone uses.
Upvotes: 0
Reputation: 1289
It is because you didn't break the execution when there is a failure. The bug is in the validateText recursion, the code should be:
return validateText();
Upvotes: 2
Reputation: 36
vaidateText() is called recursively. So, after your second "correct" input, the first "incorrect" input is returned as that first method call is finished.
Upvotes: 0
Reputation: 213311
The problem is in your else
blocks, where you are calling validateText()
recursively, and ignoring the return value. The method in that case will anyhow return the first value entered by the user. You should change the else
block to:
else {
System.out.println("Error occured, non alphabet found in text");
log("error = " + t);
return validateText(); // Return the result
}
Upvotes: 2