Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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)

 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic