0

One of my AP projects includes separating each word from a string, I have tried accomplishing numerous times yet with no success! My class has not yet learned about arrays, regex, or split yet so if you could help please avoid any of those. However we did learn substring, charAt, indexOf, length, trim ...

This is one of my attempts: (notice that in order for me to actually notice I've split them I try adding N to the string I am re-creating, which is newWord)

public class Functions {
public static String stringReversal(String word){
    if (word.length() <= 1){
        return word;
    }else{
        char c = word.charAt(0);
        return stringReversal(word.substring(1)) + c;
    }
}

public static Boolean palindrome(String word){
    Boolean palindrome;
    if (word.equalsIgnoreCase(stringReversal(word))){
        return palindrome = true;
    } else{
        return palindrome = false;
    }
}

public static String pigLatin(String sentence){
    if(sentence.length() <= 1){
        return sentence;
    } else {
       String newWord = "";
       return newWord += pigLatin(sentence.substring(0, sentence.indexOf(" "))) + " N ";
    }
}

}

Main:

public class Main {
public static void main (String [] args){
    Scanner in = new Scanner(System.in);
    String word = in.nextLine();
    System.out.println(Functions.test(word));
   }
} 

However the output only print N! Can anyone please help and show a way that I can accomplish this, I've tried many ideas yet none worked.

  • 1
    `sentence.substring(0, sentence.indexOf("")` is probably the equivalent to `sentence.substring(0, 0)` – Josh M Nov 02 '13 at 01:44
  • @hasan were not learning that until chapter 15 –  Nov 02 '13 at 01:48
  • 1
    Oh, right. "chapter 15" That says everything! (Which chapter are you on now?) – Simon Forsberg Nov 02 '13 at 01:52
  • @SimonAndréForsberg 10 –  Nov 02 '13 at 01:53
  • You know recursion but not loops? That seems strange to me. – Ryan Amos Nov 02 '13 at 01:58
  • @RyanAmos I personally know about loops from previous experience but I am trying to do it as if I didn't know them because that's how this book is written –  Nov 02 '13 at 02:00
  • 1
    That's really strange that it's taught that way. Regardless, note `sentence.substring(0, sentence.indexOf(" "))` will grab everything before the first space. Which is a single word. Don't you want to grab everything *after* the first space? – Ryan Amos Nov 02 '13 at 02:04

3 Answers3

0

Since this seems to be highly homework-related, I will only post some hints and suggestions, you will have to combine my hints and suggestions to come up with the/a solution yourself.

I believe this: sentence.indexOf("") should be this: sentence.indexOf(" ")

Checking indexOf an empty string makes not much sense (it always returns zero, since the empty string can be found everywhere within a String).

public static void main(String[] args) {
    String word = "a bit of words";
    System.out.println(test(word));
}

public static String test(String sentence){
    if(sentence.length() <= 1){
        return sentence;
    } else {
        String newWord = "";
        return newWord += test(sentence.substring(0, sentence.indexOf(" "))) + " N ";
    }
}

The above prints: a N

However, if your input is only one word then sentence.indexOf(" ") will return -1. You will need to check this. Suggestion: Modify your if-statement to instead check if string contains a space character or not.

To solve the assignment, you will need some kind of loop (recursion can also be a kind of loop) to repeat a slightly modified process for each word. Hint: Fetch the first word, then fetch the original String except for the extracted word.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • fixed that, but now I get an error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 –  Nov 02 '13 at 01:46
  • @user2895567 You'll receive that `Exception` when you get to the end of the string. This means that you are at the last word, thus no more spaces. – Josh M Nov 02 '13 at 01:47
  • @user2895567 Update your question with your new code. I don't really see how you would get that error, unless you included a -1 somewhere. – Simon Forsberg Nov 02 '13 at 01:47
  • @SimonAndréForsberg done. I uploaded my unfinished project maybe some other code above is interfering. –  Nov 02 '13 at 01:53
  • @user2895567 Updated my answer. When your String does not contain a space, you get this error. – Simon Forsberg Nov 02 '13 at 01:57
0
public static void main( String[] args )
{
    Scanner in = new Scanner( System.in );
    try
    {
        while( true )
        {
            String word = in.nextLine();
            System.out.println( splitWords( word ) );
        }
    }
    finally
    {
        in.close();
    }

}

private static String splitWords( String s )
{
    int splitIndex = s.indexOf( ' ' );
    if( splitIndex >= 0 )
        return s.substring( 0, splitIndex ) + " N " + splitWords( s.substring( splitIndex + 1 ) );
    return s;
}
LINEMAN78
  • 2,562
  • 16
  • 19
  • An answer with 100% code is normally not very helpful. It is more helpful to add a bit of description **why** you post that code, and **what** your code does. – Simon Forsberg Nov 11 '13 at 18:58
-1

you can use standard method String#split()

String[] words = sentence.split(' ');

note than words is an array

Tantowi Mustofa
  • 677
  • 4
  • 8