6 Replies - 654 Views - Last Post: 11 November 2009 - 08:35 PM Rate Topic: -----

#1 soulja1234   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 11-November 09

help needed with code

Posted 11 November 2009 - 06:31 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: help needed with code

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: help needed with code

Posted 11 November 2009 - 06:34 PM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 soulja1234   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 11-November 09

Re: help needed with code

Posted 11 November 2009 - 06:41 PM

View Postmostyfriedman, on 11 Nov, 2009 - 05:34 PM, said:

[rules][/rules]

ok i will post what i have done so far
Was This Post Helpful? 0
  • +
  • -

#4 soulja1234   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 11-November 09

Re: help needed with code

Posted 11 November 2009 - 07:47 PM

View Postsoulja1234, on 11 Nov, 2009 - 05:41 PM, said:

View Postmostyfriedman, on 11 Nov, 2009 - 05:34 PM, said:

[rules][/rules]

ok i will post what i have done so far

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
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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 :code:
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
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 soulja1234   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 11-November 09

Re: help needed with code

Posted 11 November 2009 - 08:28 PM

View Postpbl, 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 :code:
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
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1