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 said, "That's not a good idea." then the output of the program should be
He
said
that's
not
a
good
idea
An apostrophe is to be considered part of a word if there is a letter on each side of the apostrophe. ) To test whether a character is a letter, you might use (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'). However, this only works in English and similar languages. A better choice is to call the standard function Character.isLetter(ch), which returns a boolean value of true if ch is a letter and false if it is not. This works for any Unicode character. For example, it counts an accented e, é, as a letter.
help needed with code
Page 1 of 16 Replies - 654 Views - Last Post: 11 November 2009 - 08:35 PM
Replies To: help needed with code
#3
Re: help needed with code
Posted 11 November 2009 - 06:41 PM
#4
Re: help needed with code
Posted 11 November 2009 - 07:47 PM
soulja1234, on 11 Nov, 2009 - 05:41 PM, said:
package javaapplication1;
import java.util.StringTokenizer;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//Prints a sentence
System.out.print("Please enter a sentence of your choice");
System.out.println();
final String Sentence=scan.nextLine(); // u asked that
//Print each word on a separate line
StringTokenizer st = new StringTokenizer(Sentence);
while (st.hasMoreTokens() ) {
System.out.println(st.nextToken() );
}
}
}
this is what is dont understand
lets say the user enters something like a number or a character that is not a letter lets say period or question mark
how am i suppose to discard it using the standard function Character.isLetter(ch) to test whether it is not a letter
#5
Re: help needed with code
Posted 11 November 2009 - 07:57 PM
StringTokenizer st = new StringTokenizer(Sentence);
while (st.hasMoreTokens() ) {
}
You did very well until there but next time please
Here you have different opportunities
you can test if a number was entered
StringTokenizer st = new StringTokenizer(Sentence);
while (st.hasMoreTokens() ) {
String str = st.nextToken();
double x;
try {
x = Double.parseDouble(str);
// translation worked
}
catch(NumberFormatException e) {
// not a valid double
}
}
#6
Re: help needed with code
Posted 11 November 2009 - 08:28 PM
pbl, on 11 Nov, 2009 - 06:57 PM, said:
StringTokenizer st = new StringTokenizer(Sentence);
while (st.hasMoreTokens() ) {
}
You did very well until there but next time please
Here you have different opportunities
you can test if a number was entered
StringTokenizer st = new StringTokenizer(Sentence);
while (st.hasMoreTokens() ) {
String str = st.nextToken();
double x;
try {
x = Double.parseDouble(str);
// translation worked
}
catch(NumberFormatException e) {
// not a valid double
}
}
the code that i got here is printing the words from a sentence on a separate line i want when i enter an integer or something of sort that is not a letter ....that i would not print
#7
Re: help needed with code
Posted 11 November 2009 - 08:35 PM
StringTokenizer st = new StringTokenizer(Sentence);
while (st.hasMoreTokens() ) {
String str = st.nextToken();
try {
int x = Integer.parseInt(str);
System.out.println("Integer entered: " + x);
}
catch(NumberFormatException e) {
// not a valid int I do not print anything
}
}
Page 1 of 1

New Topic/Question
Reply



MultiQuote




|