1 2 Previous Next 18 Replies Latest reply: Nov 21, 2008 3:15 PM by 843785 RSS

    Random integer that is a multiple of 5?

    843785
      So far I got the program to tell me if the user's inputted letter is right or wrong, but how can I have the computer tell the user what slot the guessed letter (if correct) is in? I.e. the letter "e" is in the first and last slots.

      This is what I have so far, but all it says is the previously used wrong letters and previously guessed right letters, but not in the order of the word.
                JOptionPane.showMessageDialog (null, "You have guessed all of the following letters incorrectly: \n" + guessedWrongLetters + "\n \nYou have guessed all of the following correct letters: \n" + guessedRightLetters);
      Also, How can I generate a random integer between one and 250 that is a multiple of five? I.e. 5, 10, 15, etc. I'm using the String.substring (+start+, end) method to choose a random word from a single variable "wordlist" that contains 50 five letter words.

      This is what I have, but it just chooses at random letter for the first part of the substring, and adds five to that chosen letter for the second part. So it sometimes chooses something in the middle of a word.
      compNumOne = 1 + randy.nextInt (245);
                 compNumTwo = compNumOne + 5;
                 itsComputersWord = itsComputersWordList.substring(compNumOne, compNumTwo);
      Here is the word list:
           private String itsComputersWordList = "firstbibleshoeshoneyearthgloveapplechildshipsboatsaortaacidsallowroadsburntbusesdocksbucksbuddydudesudderembedemberelveseggedentryebonydunesdoweldozeldoorsdiverdisksdirksdinerdualsfrothelbowfryerfrozefrownfretsfumedgullsneckshairyhabitlinenlilacmedal";}
      Edited by: Pluberus on Nov 20, 2008 4:17 PM - Fixed a mistake.
        • 1. Re: Random integer that is a multiple of 5?
          843785
          Pluberus wrote:
          How can I generate a random integer between one and 250 that is a multiple of five?
          Erm... generate a random number between 1 and 50 and then multiply that random number by 5?
          • 2. Re: Random integer that is a multiple of 5?
            843785
            Wow thanks. Can't believe I didn't think of that. :D
            • 3. Re: Random integer that is a multiple of 5?
              843785
              Any help on getting the computer to say where the correct letters are placed?
              • 4. Re: Random integer that is a multiple of 5?
                843785
                why not just put the 50 words into a List and then choose a random number from 1-50 and grab that word?
                if you want to know which letters in a String match the inputed character dont over think it.
                just iterate over the String or use the indexOf() method.
                [read the doc.|http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html]
                • 5. Re: Random integer that is a multiple of 5?
                  843785
                  But indexOf is for integers? I can't get it to work.
                  • 6. Re: Random integer that is a multiple of 5?
                    796447
                    Pluberus wrote:
                    But indexOf is for integers? I can't get it to work.
                    What? String.indexOf works on a String and tells you which position in the string contains the item you're looking for.
                    • 7. Re: Random integer that is a multiple of 5?
                      843785
                      indexOf returns an integer but it is not for integers.

                      JSG
                      • 8. Re: Random integer that is a multiple of 5?
                        843785
                        Oh ok thanks. I think I figured it out. But what if the letter occurs more than one time in the string?
                                 JOptionPane.showMessageDialog (null, itsComputersWord.indexOf(itsUsersLetter));
                        • 9. Re: Random integer that is a multiple of 5?
                          796447
                          Pluberus wrote:
                          Oh ok thanks. I think I figured it out. But what if the letter occurs more than one time in the string?
                                   JOptionPane.showMessageDialog (null, itsComputersWord.indexOf(itsUsersLetter));
                          Then it returns the first position found, just like the documentation indicates. If you need to count them, then iterate over the string yourself, counting them as you go.
                          • 10. Re: Random integer that is a multiple of 5?
                            843785
                            Well, i would probably use lastIndexOf() to see if it is the last one and if not make a substring using, get this substring(int, int) and get the next one from the substring with indexOf(), but thats just me. You could skip the lastIndexOf part since in your code it isn't going to save much anyhow.

                            JSG
                            • 11. Re: Random integer that is a multiple of 5?
                              843785
                              What about something like this:?
                                              indexHolder = itsComputersWord.indexOf(itsUsersLetter);
                                              if (indexHolder == 0)
                                                  JOptionPane.showMessageDialog (null, "The letter you guessed is the first letter of the secret word.");
                                              if (indexHolder == 1)
                                                  JOptionPane.showMessageDialog (null, "The letter you guessed is the second letter of the secret word.");
                                              if (indexHolder == 2)
                                                  JOptionPane.showMessageDialog (null, "The letter you guessed is the third letter of the secret word.");
                                              if (indexHolder == 3)
                                                  JOptionPane.showMessageDialog (null, "The letter you guessed is the fourth letter of the secret word.");                
                                              if (indexHolder == 4)
                                                  JOptionPane.showMessageDialog (null, "The letter you guessed is the fifth letter of the secret word.");
                              By the way, is there an easier way to do what I just did? instead of all the multiple ifs?

                              Also, since none of my words have a letter used 3 times. Could I use the lastIndexOf and the indexOf to tell the user what position their letter is in?
                              • 12. Re: Random integer that is a multiple of 5?
                                796447
                                Pluberus wrote:
                                What about something like this:?
                                                indexHolder = itsComputersWord.indexOf(itsUsersLetter);
                                if (indexHolder == 0)
                                JOptionPane.showMessageDialog (null, "The letter you guessed is the first letter of the secret word.");
                                if (indexHolder == 1)
                                JOptionPane.showMessageDialog (null, "The letter you guessed is the second letter of the secret word.");
                                if (indexHolder == 2)
                                JOptionPane.showMessageDialog (null, "The letter you guessed is the third letter of the secret word.");
                                if (indexHolder == 3)
                                JOptionPane.showMessageDialog (null, "The letter you guessed is the fourth letter of the secret word.");                
                                if (indexHolder == 4)
                                JOptionPane.showMessageDialog (null, "The letter you guessed is the fifth letter of the secret word.");
                                By the way, is there an easier way to do what I just did? instead of all the multiple ifs?

                                Also, since none of my words have a letter used 3 times. Could I use the lastIndexOf and the indexOf to tell the user what position their letter is in?
                                Ouch. Are you considering doing that all the way to virtual infinity? Why not just show something like:
                                "The letter you guessed is in position " + (indexHolder + 1)
                                instead of converting numbers into english ordinal words. Unless you want to build that complexity into the system as well, for virtually no benefit.
                                Also, what if the letter isn't found, when indexHolder is -1? You should account for that as well.
                                • 13. Re: Random integer that is a multiple of 5?
                                  843785
                                  Well I put this in the if usersLetter is correct loop, so it doesn't matter if it is -1. And also, my words are only 5 letter words.
                                  • 14. Re: Random integer that is a multiple of 5?
                                    843785
                                    Are you considering doing that all the way to virtual infinity?
                                    I always liked Jamiroquai.

                                    ~
                                    1 2 Previous Next