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();
    } 

 }

Recommended Answers

All 2 Replies

use in.nextLine() (line 14 of your code)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.