View Single Post
Old Apr 12th, 2008, 11:40 PM   #7
manny85
Newbie
 
manny85's Avatar
 
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0 manny85 is on a distinguished road
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.


java Syntax (Toggle Plain Text)
  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

Last edited by manny85; Apr 12th, 2008 at 11:54 PM. Reason: Adjusting Code Format
manny85 is offline   Reply With Quote