Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 12th, 2008, 8:02 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 80
Rep Power: 7 kewlgeye is on a distinguished road
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.



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. 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. }

Last edited by Ancient Dragon; Apr 12th, 2008 at 11:26 PM. Reason: corrected code tags
kewlgeye is offline   Reply With Quote
Old Apr 12th, 2008, 9:40 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,428
Rep Power: 15 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 12th, 2008, 9:55 PM   #3
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 80
Rep Power: 7 kewlgeye is on a distinguished road
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.
kewlgeye is offline   Reply With Quote
Old Apr 12th, 2008, 11:28 PM   #4
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
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.
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Apr 12th, 2008, 11:45 PM   #5
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 80
Rep Power: 7 kewlgeye is on a distinguished road
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?

java Syntax (Toggle Plain Text)
  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. }
kewlgeye is offline   Reply With Quote
Old Apr 12th, 2008, 11:59 PM   #6
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 11 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Apr 13th, 2008, 12:40 AM   #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 13th, 2008 at 12:54 AM. Reason: Adjusting Code Format
manny85 is offline   Reply With Quote
Old Apr 13th, 2008, 1:15 AM   #8
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 11 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Re: Random "Letter" Generator

Quote:
Originally Posted by manny85 View Post
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.
titaniumdecoy is offline   Reply With Quote
Old Apr 13th, 2008, 1:55 AM   #9
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

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).
manny85 is offline   Reply With Quote
Old Apr 13th, 2008, 2:00 AM   #10
titaniumdecoy
Programming Guru

 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Location: California
Posts: 1,535
Rep Power: 11 titaniumdecoy will become famous soon enoughtitaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Templated Random Number Generator Ben.Dougall C++ 5 Jul 6th, 2007 8:42 PM
random Numbers in openGL csrocker101 C++ 5 Apr 24th, 2007 9:02 PM
[vb6] need advice concerning a largescale non-recurring random number generator chepfaust Visual Basic 8 Jun 8th, 2006 6:04 AM
[Python] Password Generator bulio Show Off Your Open Source Projects 2 Feb 28th, 2006 4:01 AM
PHP random quote generator script grimpirate PHP 10 Sep 24th, 2005 10:22 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:13 AM.

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