Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent help! my while loop is not working

 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/**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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while(quit!="the end")

should be
while(!quit.equalsIgnoreCase("the end"))
 
unyime inok
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the program doesn't drop out of the loop when a user enters "the end", even with equalsIgnoreCase().
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic