This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes urgent help! my while loop is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "urgent help! my while loop is not working" Watch "urgent help! my while loop is not working" New topic
Author

urgent help! my while loop is not working

unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
/**Counting the appearances of a given word in a text
We have seen how to read in and manipulate characters and character strings and how to use the classes String and StringTokenizer. Use these to read in a word and then other text from the keyboard. Count the occurrences of the word that you read in at the beginning. Your program should be case insensitive; this means that you should convert the original word to pure upper case OR lower case and compare each token that you separate to this same case using toUpperCase() or toLowerCase(). Your program should keep checking text until you type �The End� by itself on one line**/


import java.io.*;
import java.util.*;
//Inserts word into text and breaks the text into tokens
public class WordText{
String word = new String();
String text = new String();


//Allows user to enter input from the keyboard
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader keyb = new BufferedReader(isr);

//sets word
public void setWord(String w)
{
word = w;
}

//gets word
public String getWord() throws IOException
{
return word;
}

//sets text
public void setText(String t)
{
text = t;
}

//gets text
public String getText() throws IOException
{
return text;
}

//Edits the text entered
public static void editText(String word, String text) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader keyb = new BufferedReader(isr);

String edittedText = new String(),
quit = new String();
int indexOfxxx;
while(quit!="the end")
{
System.out.println("Type 'the end' to quit checking");
quit = keyb.readLine();
while((indexOfxxx = text.indexOf("xxx"))!= -1)
{
edittedText = text.substring(0,indexOfxxx);
edittedText = edittedText.concat(word);
edittedText = edittedText.concat(text.substring(indexOfxxx+3));

text = edittedText;
indexOfxxx =text.indexOf("xxx");
System.out.println(edittedText.toUpperCase());

System.out.println("Type 'the end' to quit checking");
quit = keyb.readLine();

//Breaks text into tokens
StringTokenizer st = new StringTokenizer(edittedText);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken().toUpperCase());
}

}//end of inner-while loop

}//end of outer-while loop

}


//Prompts user for input and reads it
public static void readInput()throws IOException
{
WordText wt = new WordText();

System.out.println("Enter a word");
wt.word = wt.keyb.readLine();
wt.getWord();

System.out.println("Please enter the text with xxx that you want to replace");
wt.text = wt.keyb.readLine();
wt.getText();
wt.editText(wt.word,wt.text);
}

//Main method
public static void main(String [] args)throws IOException
{
readInput();
}
}//end of class
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
while(quit!="the end")

should be
while(!quit.equalsIgnoreCase("the end"))
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
the program doesn't drop out of the loop when a user enters "the end", even with equalsIgnoreCase().
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
It doesn't drop out of the inner loop if there are still occurrences of "xxx"
but it does quit OK when there are no more "xxx".

Your program doesn't seem to follow the specs at the top of your post,
here's a few lines, just add the 'count' method (if this is what you're
trying to do)

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: urgent help! my while loop is not working
 
Similar Threads
input from console
please please..make this in applet.
Reading from keyboard
Help wanted to put correct Loop type....
i guess using variables from one class into another class all in the same package?