DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2005

    where to put my input reader??? in what class should it go?

    im currently making a game of hangman in java.

    i currently have a menu which allows you to add a word and quit the game

    unfortunately i am only able to add one word at the moment, i would like to add more.

    i currently have a print out which says:

    "word has succesfully been added to the list
    would you like to add another?
    Y or N"

    so then i would like to return back to the menu if the response is N
    and when the user types in Y i would like to add a new word.

    i would also like to do something similar with quiting

    this is the codeing im using and is from my menu class

    // this deals with adding a word
    // the user presses 2 on the menu
    // prints enter a word
    if (response.equals("2")) {
    System.out.println("enter a word");
    String newword = in.readLine();

    this then goes to my word book class does this method to check if the word doesn't already exist, i am using an arraylist here.

    //this is how to add a new word
    public void addWord(String word)
    { boolean duplicates = false ;
    for(int i = 0; i < words.size(); i++)
    if (words.get(i).toString().equals(word)) {
    duplicates = true;
    System.out.println ( "________________________");
    System.out.println ( " Word already exists");
    System.out.println ( " and has been");
    System.out.println ( " EXTERMINATED");
    System.out.println ( " add a new word");
    System.out.println ( "________________________");
    if(duplicates == false){
    System.out.println ( "________________________");
    System.out.println ( " Word has been added ");
    System.out.println ( " to the list");
    System.out.println ( "_______________________________");
    System.out.println ( "Do you wish to add more words ?");
    System.out.println ( "_____________Y / N_____________");


    if you can help in any way that is great. Thankyou very much

    all the best dom

  2. #2
    Join Date
    Nov 2004
    Place the input procedure in a while(true) -loop, with the prompt & input on
    top, and that breaks when the user enters the quit option.

    Btw, if you are using an arraylist of strings you don't have to loop the list
    to check for duplicated strings, that is done for you by the ArraList class
    when you use its public boolean contains(Object ob) method.

    If you store objects of your own custom class in an ArrayList, then you will
    have to implement (override) the boolean equals(Object ob) method
    in that class to achieve this.
    eschew obfuscation

  3. #3
    Join Date
    Apr 2005

    well now i can do this

    right i can now continually add words by adding this code

    String add_word_again="y";
    while (add_word_again.equals("y"))

    but i am unable to go back to my menu
    and the code only recongises a small y and not a capital Y

    is there anything you can suggest all the best dom

  4. #4
    Join Date
    Nov 2004
    If you post your complete code here I'll tell you.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

By using this site, you agree to the Privacy Policy