7 Replies - 4834 Views - Last Post: 07 March 2008 - 02:26 AM Rate Topic: -----

#1 anorman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-January 08

Prompt user for words (use loop) then form sentence from words

Posted 06 March 2008 - 08:58 PM

Im trying to write a program that prompts the user for words until they type "stop", "done", or "end" and prints all the words in a sentence except "a", "an", and "the"

This is what I have so far:
 import java.util.Scanner;

public class InputFilter {
	public static void main(String[] args){
	Scanner keyboard = new Scanner(System.in);
	
	String word;
	
	System.out.print("Enter the words 'stop' 'end' or 'done' when finished");
		word = keyboard.next();
		while ( !word.equalsIgnoreCase(("stop")){ ;
			System.out.println();
			word = keyboard.next();
		} 
		
	}
}


This...sucks so far. Tried to use ("stop")||("end")||("done") but it didnt like it. I thought maybe I could first put all the words into a sentence then use str.replaceAll to replace "a" "an" and "the" with blank spaces but I cant even mess with that until I can get all the words input. The best Ive been able to do is get to return the last word put it or return all the words separately.

Any help would be awesome because I have no idea and I cant seem to find anything online quite like this

Is This A Good Question/Topic? 0
  • +

Replies To: Prompt user for words (use loop) then form sentence from words

#2 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Prompt user for words (use loop) then form sentence from words

Posted 06 March 2008 - 11:50 PM

you were missing a ')'

stop")){ ; should be stop")))

bt for you assignment why dont you just have the user type in a sentance and then hit enter when they are done? and then from there take the string and split() it into an array.
Scanner keyboard = new Scanner(System.in);
	
	String word;
	String[] splitted; //use an array to store the word
	
	System.out.println("Enter the word to be formated");
		
	word = keyboard.nextLine();  // this will take in the whole line instead of the individul words the user enters

	splitted = word.split(" "); split everything at the " "  this turns every array index into  a word from the sentance
	String sentance  = ""; create an empty sentance
	
	for(int i = 0;i<splitted.length;i++){
		// this is where ill leave you in here you need to test every index of the array and add it to the senance varible if it isnt "an" "a" or "the" im sure ull figure it out i have it working on my comp so if u need help later let me know bt this should be more than enough to get you started
		}
	}




i left you mega coments and the last problem
your main issues wih the 1 above was you werent using the method nextLine() instead you were using next()
major differance between the 2 is next() take only then next word the user enters
nextLine() takes every thing until the user hits enter

your other problem was you didnt know about split(String arg).
this method takes you string arg and splits your string around that.. so for example:
String luke = "i am your father"
String[] splitted = luke.split("a")

----------
this would give:
splitted[0] = i
splitted[1] = m your f
splitted[2] =ther

notice that you actualy lose the "a" character.. so be careful when you use it with the " " because you lose all your spaces'. anyway if you need more help just post ill be back later gonna go watch machine head and killswitch now!!
Was This Post Helpful? 0
  • +
  • -

#3 anorman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-January 08

Re: Prompt user for words (use loop) then form sentence from words

Posted 06 March 2008 - 11:55 PM

Well that would work but it needs to use a loop and enter one word at a time. Thanks though!
Was This Post Helpful? 0
  • +
  • -

#4 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Prompt user for words (use loop) then form sentence from words

Posted 07 March 2008 - 12:04 AM

k thennn...

Scanner scan = new Scanner(System.in);
	String sentance = "";
	String word = "";
	while(true){
		word = scan.next();
		if ((word.equals("stop"))||(word.equals("done"))||(word.equals("end"))){
			break;
		}
		else{
			sentance = sentance + word+ " ";
		}
	}
	System.out.println(sentance);
}


then use this :P
i would strongly recomend you use nextLine() though.. its way more code and user friendly


i ws thinking a better way 2 do this would be to add an elseif clause to the code that tests "word" to c if its "an" "a" or "the" then if it is dont add it 2 the sentance

This post has been edited by gl3thr0: 07 March 2008 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 anorman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-January 08

Re: Prompt user for words (use loop) then form sentence from words

Posted 07 March 2008 - 01:07 AM

That did the trick thank you so much!
Was This Post Helpful? 0
  • +
  • -

#6 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Prompt user for words (use loop) then form sentence from words

Posted 07 March 2008 - 01:36 AM

ny time
Was This Post Helpful? 0
  • +
  • -

#7 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Prompt user for words (use loop) then form sentence from words

Posted 07 March 2008 - 02:19 AM

i appreciate the sentiment of your 3 commandments.
but the footer is too big.
Was This Post Helpful? 0
  • +
  • -

#8 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Prompt user for words (use loop) then form sentence from words

Posted 07 March 2008 - 02:26 AM

View Postcapty99, on 7 Mar, 2008 - 02:19 AM, said:

i appreciate the sentiment of your 3 commandments.
but the footer is too big.


does this work?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1