Need help with my program? (Breaking compound sentences)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Need help with my program? (Breaking compound sentences)

Hybrid View

  1. #1
    Join Date
    Sep 2012
    Posts
    3

    Need help with my program? (Breaking compound sentences)

    I am having trouble making this code behave correctly.
    I am trying to split up a compound sentence assuming:
    1. AND is the only word that can create compound sentences.
    2. Any time that AND is used it is a compound sentence.
    3. A compound sentence can only have 1 AND in it.

    When I run the program and type in a sentence, the result only returns the first word in the sentence. Please help. My java skills are a little rusty.

    Code:
    import java.util.StringTokenizer;
    import java.util.Scanner;
     
    public class BreakComp {
     
            public static void main(String[] args) {
    	        
    	        	Scanner user_input = new Scanner(System.in);
    	        
    				String sentence;
    				System.out.println("Please enter a compound sentence: ");
    				sentence = user_input.next();
    	        
                    if(sentence.contains("and")){
                            sentence = sentence.replace(" and ", "|");
                            System.out.println(tokenizeSentence(sentence)[0]);
                            System.out.println(tokenizeSentence(sentence)[1]);
                    } else{
                            System.out.println(sentence.toUpperCase());
                    }
            }
           
            public static String[] tokenizeSentence(String s){
                    String[] sentenceString = new String[2];
                    StringTokenizer tok = new StringTokenizer(s, "|");
                    sentenceString[0] = tok.nextToken();
                    sentenceString[1] = tok.nextToken();
                    return sentenceString;
            }
     
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need help with my program? (Breaking compound sentences)

    Always read the API docs before using a class. The docs for Scanner say "A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace." and for the next() method it says "Finds and returns the next complete token from this scanner". Which means calling next() will return one word starting with the first word.
    The thing you should do when something doesn't work as your expected is to add some System.out.println("...") statements which prints a message and the value of key variables at various points in the code. This will help you to pin down where things are going wrong.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2012
    Posts
    3

    Re: Need help with my program? (Breaking compound sentences)

    Quote Originally Posted by keang View Post
    Always read the API docs before using a class. The docs for Scanner say "A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace." and for the next() method it says "Finds and returns the next complete token from this scanner". Which means calling next() will return one word starting with the first word.
    The thing you should do when something doesn't work as your expected is to add some System.out.println("...") statements which prints a message and the value of key variables at various points in the code. This will help you to pin down where things are going wrong.
    I realized that just now, I changed it to nextLine and everything is working perfectly. Thanks!

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: Need help with my program? (Breaking compound sentences)

    I would recommend testing your code with the following input string: "My hand hurts".
    The code you presented won't work properly for this - I'll leave it to you to find out why

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center