0

I have an assignment an I have been looking at it for a while now but really have no idea what I should be doing. I have included the code I have written but is will only print out He and not the entire phrase.

Write a program that reads one line of input text and breaks it up into words. The words should be output one per line. A word is defined to be a sequence of letters. Any characters in the input that are not letters should be discarded. For example, if the user inputs the line He asked, "Is that a good idea?." then the output of the program should be

He
asked
Is
that
a
good
idea

To test whether a character is a letter, please use the standard function Character.isLetter(ch), which returns a boolean value of ture if ch is a letter and false if it is not.

import java.util.Scanner;
public class Phrase
{
	public static void main(String[] args) {
	       
        String line;    
        int i;         
        char ch;
        boolean didCR;  
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a line of text.");
        line = in.next();
        didCR = true;
        
        for ( i = 0;  i < line.length();  i++ ) {
           ch = line.charAt(i);
           if ( Character.isLetter(ch) ) 
           {
              System.out.print(ch);
              didCR = false;
           }
           else {
              if ( didCR == false ) 
              {
                 System.out.println();
              }
           }
        }
        
        System.out.println();
    } 

 }
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by lbgladson
0

Wow, that was easy and I had no idea if any of the code I had written was correct. Makes me happy to know I was close! Thanks so much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.