Results 1 to 4 of 4
  1. #1
    NotTengu is offline Member
    Join Date
    Feb 2016
    Rep Power

    Default RtP: Split a Sentence into Individual Words

    I'm new here, and I invariably break some code of conduct or something every time I join a new forum, so I apologize in advance for that. Now, on to the question.

    I'm using Ready to Program Version 1.7.1 (not by choice, the teacher requires it). While I am aware that everyone hates Ready, that's what I'm using, so deal with it.

    I have an assignment that I'm not quite sure how to complete. I've got some of the problem down, but I'm getting a weird error that I don't know how to remedy. The assignment is as follows: The program must ask the user to enter a sentence. Once the user enters a sentence, the program will break the sentence into individual words and print each word on a new line. Although the teacher recommends using the Character.isLetter(ch) method thingy, I prefer .charAt. I'm trying to make my code scan until it finds a whitespace, then print out all characters between the beginning of the word (wordStart is a variable I have declared) and the whitespace. The program should then move wordStart's value to the whitespace, and the program will recommence, scanning until it once again finds a whitespace. This program repeats until it reaches the end of the sentence.

    I'm not sure if there's some way to put java syntax directly into this post, so I'll just copy/past my code here. It's been a while since I used Vbulletin, so I'm not sure how to put it in a spoiler. Sorry.

    Java Code:
    import java.awt.*; //Importing user input stuff
    import hsa.Console;
    public class C4A6SBA {//this is a class
        static Console c = new Console (); //more console stuff
        static public void main (String [] args) {//main thingy
            c.println ("Welcome! Please enter a sentence. This program will break it up into indiviudal");
            c.println ("words! Each word will be on a new line. Please enter your sentence now."); //greeting user.
            StringBuffer uS = new StringBuffer ("");//uS stands for userSentence, but is shorter and easier to type. It's blank right now because its value needs to be assigned on a different line.
            uS.append (c.readString()); //assigning value. On a different line because it's a user input and not an absolute value.
            int counter = 0;//my counter
            int wS = 0; //stands for wordStart. It shows where each word starts.
            do { //begin do loop 1
                    if (uS.charAt(counter)==(' ')){ //this line basically scans along the sentence until it finds a space.
                    StringBuffer temporary = new StringBuffer (""); //not sure if this is overly complicated. creates a new SB that will be printed an then have its value changed later
                    temporary.append (uS.substring(wS,(counter))); //giving the SB its text. this needs to be on a different line because SBs are weird.
                        c.println(temporary+""); //I added a single nothing in order for Java to understand that I want to print a string instead of some other thing. It was giving me a weird error.
                        wS = counter; //moving the start of the next word forward to the point where the previous word ends.
                    }//end the if loop
                    counter++; //increasing counter
            } while (counter < uS.length());{} //this program stops when the counter reaches the end of the sentence.
        }//end main
    }//end class
    So that's the code. It currently accepts the user input, but then outputs nothing. I'm not sure what to do. Thanks in advance to anyone who helps, and I apologize in advance for inevitably violating forum rules.
    Last edited by NotTengu; 02-05-2016 at 10:18 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: RtP: Split a Sentence into Individual Words

    Please edit your post and wrap your code with code tags:


    to get highlighting and preserve formatting.

    I'm getting a weird error
    Please explain.
    If you are getting error messages, copy the full text and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    NotTengu is offline Member
    Join Date
    Feb 2016
    Rep Power

    Default Re: RtP: Split a Sentence into Individual Words

    Oops. I made a mistake while making the post. I wrote the introductory paragraph, then went to copy/paste my code, but the found the solution to my error, fixed it, then copy/pasted the code without changing the introductory paragraph. The current problem is that there is no output regardless of what the user inputs.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: RtP: Split a Sentence into Individual Words

    Do some debugging.
    Print out the values of the different variables, and additional println's that show where int he code you are.

    One thing, there's no point using that first StringBuffer (uS). Just use the String that comes from the readLine() directly.
    Since you're not adding anything to the StringBuffer other than that initial String it's not needed.

    And it should be StringBuilder unless it needs to handle multiple threads, which it doesn't in this case (or 99% of cases).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. how to reverse the order of words in a sentence?
    By dksellou in forum New To Java
    Replies: 6
    Last Post: 09-05-2014, 07:04 PM
  2. Replies: 3
    Last Post: 10-26-2013, 08:21 PM
  3. Replies: 8
    Last Post: 08-29-2013, 03:45 PM
  4. Replies: 3
    Last Post: 07-08-2012, 11:08 PM
  5. Replies: 0
    Last Post: 02-25-2010, 12:51 PM

Posting Permissions

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