Welcome to Dream.In.Code
Become a Java Expert!

Join 149,832 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,529 people online right now. Registration is fast and FREE... Join Now!




Displaying all loop outputs?

 
Reply to this topicStart new topic

Displaying all loop outputs?

Tomsta11
15 Feb, 2007 - 06:45 PM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 2


My Contributions
I was wondering how you get java to print all the outputs achieved during the process of the loop. I am working on a class assignment in which the user inputs multiple, single words using scanner and loop.
CODE
import java.util.Scanner;

public class InputFilter
{
        public static void main(String[]args)
        {
                Scanner kb = new Scanner(System.in);
                System.out.println("Enter words and type 'stop' 'done' or 'end' when finished:");
                String word = kb.next();
                while (!word.contains("stop") && !word.contains("end") && !word.contains("done"))
                {

The program is supposed to stop and display the words in a sentence, taking out the words "a", "an", and "the". So an entry like..
This
dog
is
a
fast
one
-would be "This dog is fast one"
but I don't know how to string all the entries together, some help?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Displaying All Loop Outputs?
15 Feb, 2007 - 06:57 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
You could try something like
CODE

String wholestr;
Scanner kb = new Scanner(System.in);
                System.out.println("Enter words and type 'stop' 'done' or 'end' when finished:");
                String word = kb.next();
                while (!word.contains("stop") && !word.contains("end") && !word.contains("done"))
                {
                   wholestr = wholestr + " " + word;
                   word = kb.next();
                 }
                 System.out.println(wholestr);

although doing it this way is pretty expensive as far as processing is concerned, as java strings are immutable.

I'd advise using the StringBuffer class, specifically the append method.

http://java.sun.com/j2se/1.4.2/docs/api/ja...va.lang.String)

User is online!Profile CardPM
+Quote Post

Tomsta11
RE: Displaying All Loop Outputs?
15 Feb, 2007 - 07:44 PM
Post #3

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 2


My Contributions
i ended up doing it like this and it seems to be working alright:
CODE
import java.util.Scanner;

public class InputFilter
{
        public static void main(String[]args)
        {
                Scanner kb = new Scanner(System.in);
                System.out.println("Enter words and type 'stop' 'done' or 'end' when finished:");
                String word = kb.next();
                String full = new String();
                while (!word.contains("stop") && !word.contains("end") && !word.contains("done"))
                {
                    if ((!word.contains("a"))&&(!word.contains("the"))&&(!word.contains("an")))
                    {
                        full += " " + word;
                    }
                    word = kb.nextLine();
                }
                System.out.println(full);

        }
}


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Displaying All Loop Outputs?
15 Feb, 2007 - 07:46 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
Yeah, that was my first example, and it will work, no problem...it's just that it's pretty heavy on the processing cycles.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:36AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month