-
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();
words.addWord(newword);
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){
words.add(word);
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
-
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
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks