Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Random "Letter" Generator (http://www.programmingforums.org/showthread.php?t=15601)

kewlgeye Apr 12th, 2008 7:02 PM

Random "Letter" Generator
 
Ok folks,

I have been working on this code for a long time, first it was a random number generator, and now I am trying to modify my code to generate a letter between A-J, or 10 letters, I thought I had this nailed, but I was wrong, I am very knew to java, so I don't really understand what I am doing, I know I received an error about string comparison, or something like that and it points to the "*" in the random generator part. I don't know what to do, I tried storing the string Letters in num, and then generating a random number that would recognize when a letter was inputted. Please help. This code is driving me crazy. :)



:

  1. import java.util.*;
  2. import javax.swing.JOptionPane;
  3.  
  4.  
  5. public class guessing2
  6. {
  7. static Scanner console = new Scanner(System.in);
  8.  
  9. public static void main (String[] args)
  10. {
  11. //declare the variables
  12. int num; //variable to store the random number
  13. Double guess; // variable to store the number guess by user
  14. boolean done;
  15. String input;
  16. String correct;
  17. String coroutput;
  18. String wrong;
  19. String yguess;
  20. int noOfGuesses = 0;
  21. String[] Letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
  22.  
  23.  
  24. Letters[num] = (Math.random() * 10);
  25. done = false;
  26.  
  27. while ((noOfGuesses < 3) && (!done))
  28. {
  29. yguess = JOptionPane.showInputDialog("Please enter a letter beween A-J or number between 1-10");
  30. guess = Double.parseDouble(yguess);
  31. noOfGuesses++;
  32.  
  33. if (guess == num)
  34.  
  35. {
  36.  
  37. coroutput = "Congratulations you guessed it";
  38. JOptionPane.showMessageDialog(null, coroutput,
  39.         "Congratulations", JOptionPane.INFORMATION_MESSAGE);
  40. done = true;
  41. }
  42. else
  43. if (guess < num)
  44. {
  45. JOptionPane.showMessageDialog(null, "You're too low!",
  46.         "Failure", JOptionPane.INFORMATION_MESSAGE);
  47. }
  48. else
  49. if (guess > num)
  50. {
  51. JOptionPane.showMessageDialog(null, "You're too high!",
  52.         "Failure", JOptionPane.INFORMATION_MESSAGE);
  53. }
  54. else
  55. if (!done)
  56. {
  57. JOptionPane.showMessageDialog(null, "You lose, Too many Tries, Correct number = " + num,
  58.         "Failure", JOptionPane.INFORMATION_MESSAGE);
  59. }
  60.  
  61.  
  62. } //end while
  63. }
  64. }


Ooble Apr 12th, 2008 8:40 PM

Re: Random "Letter" Generator
 
You need to sort out your code tags (or get a moderator to do it if you can no longer edit your post). Delete the current tags, highlight the code and click the [#] button on the toolbar.

kewlgeye Apr 12th, 2008 8:55 PM

Re: Random "Letter" Generator
 
I will remember this next time, I am not able to fix it myself, but from the looks of it everything printed fine this time, but I will try again next time to do it right. I made this mistake before and thought I was doing it right, but I guess I messed up. Still open to suggestions regarding the post though.

Ancient Dragon Apr 12th, 2008 10:28 PM

Re: Random "Letter" Generator
 
you almost got the code tags right. Here is how to do it. Note that there are no spaces anywhere within the brackets.

[code=java]
// your code here
[/code]

I fixed up the code tags, but they don't help when you have crappy formatting style.

kewlgeye Apr 12th, 2008 10:45 PM

Re: Random "Letter" Generator
 
I have been working more on this, and here is the code I have now. But I am receiving this error.

"operator * cannot be applied to double.java.lang.String[]"

I can't figure it out, does any one know how I can fix this?

:

  1. import java.util.*;
  2. import javax.swing.JOptionPane;
  3.  
  4.  
  5. public class guessing
  6. {
  7. static Scanner console = new Scanner(System.in);
  8.  
  9. public static void main (String[] args)
  10. {
  11. //declare the variables
  12. int num; //variable to store the random number
  13. double guess; // variable to store the number guess by user
  14. boolean done;
  15. String input;
  16. String correct;
  17. String coroutput;
  18. String wrong;
  19. String yguess;
  20. int noOfGuesses = 0;
  21. String[] max = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
  22.  
  23. int rand = (int) (Math.random() * max)+1;
  24. num = (int) (Math.random() * 10);
  25. done = false;
  26.  
  27. while ((noOfGuesses < 3) && (!done))
  28.         {
  29.                 yguess = JOptionPane.showInputDialog("Please enter a letter beween A-J or number between 1-10");
  30.                 guess = Double.parseDouble(yguess);
  31.                 noOfGuesses++;
  32. if (guess == num)
  33.         {
  34.                 coroutput = "Congratulations you guessed it";
  35.                 JOptionPane.showMessageDialog(null, coroutput,
  36.                 "Congratulations", JOptionPane.INFORMATION_MESSAGE);
  37.                 done = true;
  38.         }
  39. else
  40. if (guess < num)
  41.         {
  42.                 JOptionPane.showMessageDialog(null, "You're too low!",
  43.                 "Failure", JOptionPane.INFORMATION_MESSAGE);
  44.         }
  45. else
  46. if (guess > num)
  47.         {
  48.                 JOptionPane.showMessageDialog(null, "You're too high!",
  49.                 "Failure", JOptionPane.INFORMATION_MESSAGE);
  50.         }
  51.  
  52. if (noOfGuesses == 3)
  53.         {
  54.                 JOptionPane.showMessageDialog(null, "You lose, Too many Tries, Correct number = " + num,
  55.                 "Failure", JOptionPane.INFORMATION_MESSAGE);
  56.         }
  57.  
  58.  
  59.         } //end while
  60.     }
  61. }


titaniumdecoy Apr 12th, 2008 10:59 PM

Re: Random "Letter" Generator
 
Look at line 23. You are multiplying Math.random() by a String[] (max), exactly as the error message states. You probably meant to write max.length.

manny85 Apr 12th, 2008 11:40 PM

Re: Random "Letter" Generator
 
First post on this forum but I have been lurking for a while. It has been a few years since I have used Java so thought I'd give this a go for some practice. Here is what I came up with based largely on your code. References are at the bottom.


:

  1. import java.util.*;
  2. import javax.swing.JOptionPane;
  3.  
  4.  
  5. public class guessing2
  6. {
  7. static Scanner console = new Scanner(System.in);
  8.  
  9. private static boolean isNumeric(String checkStr)
  10. // determine if string is numeric
  11. {
  12.         try{
  13.                 Integer.parseInt(checkStr);
  14.                 return true; //Did not throw, must be a number
  15.         }catch(NumberFormatException err){
  16.                 return false; //Threw, So is not a number
  17.         }
  18. }
  19. public static void main (String[] args)
  20. {
  21.  
  22.         int numGuesses = 0, uiInt = 0;
  23.         boolean done = false;
  24.         int rndNum = (int)(10.0 * Math.random());
  25.         String uiStr = "", rndGuess = "";
  26.         char firstChar;
  27.         String[] Letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
  28.         rndGuess = Letters[rndNum];
  29.  
  30.         while ((numGuesses < 3) && !done)
  31.         {
  32.                 uiStr = JOptionPane.showInputDialog("Please enter a letter beween A-J or number between 1-10");
  33.                 uiStr = uiStr.toUpperCase();
  34.                 if(isNumeric(uiStr))
  35.                 // check if user input was numeric
  36.                 // parse string input to integer and subtract
  37.                 // one in order to compare with rndNum
  38.                 // retrieve letter based on numeric input to
  39.                 // compare against rndGuess
  40.                 {
  41.                         uiInt = Integer.parseInt(uiStr) - 1;
  42.                         uiStr = Letters[uiInt];
  43.                 }
  44.                 else
  45.                 {
  46.                 // if is isn't numeric pull first character
  47.                 // from user input into firstChar
  48.                 // then based on that store the numeric value
  49.                 // in uiInt for comparison with rndNum
  50.                         firstChar = uiStr.charAt(0);
  51.                         switch(firstChar)
  52.                         {
  53.                         case 'A':
  54.                                 uiInt = 0;
  55.                                 break;
  56.                         case 'B':
  57.                                 uiInt = 1;
  58.                                 break;
  59.                         case 'C':
  60.                                 uiInt = 2;
  61.                                 break;
  62.                         case 'D':
  63.                                 uiInt = 3;
  64.                                 break;
  65.                         case 'E':
  66.                                 uiInt = 4;
  67.                                 break;
  68.                         case 'F':
  69.                                 uiInt = 5;
  70.                                 break;
  71.                         case 'G':
  72.                                 uiInt = 6;
  73.                                 break;
  74.                         case 'H':
  75.                                 uiInt = 7;
  76.                                 break;
  77.                         case 'I':
  78.                                 uiInt = 8;
  79.                                 break;
  80.                         case 'J':
  81.                                 uiInt = 9;
  82.                                 break;
  83.                         default:
  84.                                 JOptionPane.showMessageDialog(null, "Invalid Character Input", "Invalid Input", JOptionPane.INFORMATION_MESSAGE);
  85.                                 done = true;
  86.                                 break;
  87.                         }
  88.                 }
  89.  
  90.                         if (uiInt < rndNum | uiStr.compareTo(rndGuess) < 0)
  91.                                 JOptionPane.showMessageDialog(null, "You're too low!", "Failure", JOptionPane.INFORMATION_MESSAGE);
  92.                         else
  93.                         if (uiInt > rndNum | uiStr.compareTo(rndGuess) > 0)
  94.                                 JOptionPane.showMessageDialog(null, "You're too high!", "Failure", JOptionPane.INFORMATION_MESSAGE);
  95.                         else
  96.                                 JOptionPane.showMessageDialog(null, "Congratulations you guessed it", "Congratulations", JOptionPane.INFORMATION_MESSAGE);
  97.                                 done = true;
  98.                         numGuesses++;
  99.                 } //end while
  100.  
  101.         if (numGuesses == 3)
  102.                 JOptionPane.showMessageDialog(null, "You lose, Too many Tries, Correct Letter = " + Letters[rndNum] + " and Correct Number = " + rndNum++, "Failure", JOptionPane.INFORMATION_MESSAGE);
  103.         }
  104.         }


The reason I choose the Switch statement was mainly because I don't think that if a user inputs a letter that ".parseDouble" converts it to a numeric value. So my reasoning was that if they only guess letters they'll never succeed with that system. Please correct me if I'm wrong in that logic.

-Manny

http://www.leepoint.net/notes-java/a...andom-api.html

http://bytes.com/forum/thread17584.html ---isNumeric() function
http://www.roseindia.net/java/master...twise-or.shtml
http://www.leepoint.net/notes-java/d...goverview.html
http://www.javabeginner.com/java-string-comparison.htm
http://forum.java.sun.com/thread.jsp...sageID=9698690 ---Converting letters to numbers
http://java.sun.com/docs/books/tutor...ipstrings.html

titaniumdecoy Apr 13th, 2008 12:15 AM

Re: Random "Letter" Generator
 
Quote:

Originally Posted by manny85 (Post 143811)
The reason I choose the Switch statement was mainly because I don't think that if a user inputs a letter that ".parseDouble" converts it to a numeric value. So my reasoning was that if they only guess letters they'll never succeed with that system. Please correct me if I'm wrong in that logic.[/url]

The Double.parseDouble function will convert a String to a double. However, you are correct in that floating-point numbers are inherently inexact and as such any comparisons involving a floating-point number is likely to yield incorrect results.

manny85 Apr 13th, 2008 12:55 AM

Re: Random "Letter" Generator
 
Actually I don't think I made it very clear what I meant now that I reread my last post. I agree that using Double.parseDouble will convert a string, which contains a number (1-10 in our case), to an actual double. However if a string is storing a set a characters, in this case A-J, then I don't think Double.parseDouble will take that character and convert it to its/a numerical form. If it does in fact do that what values do the A-J end up being (numerically)...0-9, 1-10, ASCII values? Hope that makes more sense and also explains to kewlgeye why I choose the switch statement because I don't think his current program will respond correctly if a user inputs a character.

Side note: Kewlgeye, are you trying to generate two separate random values meaning a random letter and a separate random number? In my code above I'm using the randomly generated number to retrieve the corresponding letter. So if a user inputs either the correct number or letter they win (1 or A, 2 or B, 3 or C, ... , 10 or J).

titaniumdecoy Apr 13th, 2008 1:00 AM

Re: Random "Letter" Generator
 
Double.parseDouble will throw a NumberFormatException if it is unable to parse its input as a double. It is very easy to find this information in the docs.


All times are GMT -5. The time now is 9:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC