![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2008
Posts: 53
Rep Power: 1
![]() |
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)
Last edited by Ancient Dragon; Apr 12th, 2008 at 10:26 PM. Reason: corrected code tags |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jan 2008
Posts: 53
Rep Power: 1
![]() |
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.
|
|
|
|
|
|
#4 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4
![]() |
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.
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2008
Posts: 53
Rep Power: 1
![]() |
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)
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0
![]() |
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)
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 |
|
|
|
|
|
#8 | |
|
Expert Programmer
|
Re: Random "Letter" Generator
Quote:
|
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0
![]() |
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). |
|
|
|
|
|
#10 |
|
Expert Programmer
|
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Templated Random Number Generator | Ben.Dougall | C++ | 5 | Jul 6th, 2007 7:42 PM |
| random Numbers in openGL | csrocker101 | C++ | 5 | Apr 24th, 2007 8:02 PM |
| [vb6] need advice concerning a largescale non-recurring random number generator | chepfaust | Visual Basic | 8 | Jun 8th, 2006 5:04 AM |
| [Python] Password Generator | bulio | Show Off Your Open Source Projects | 2 | Feb 28th, 2006 3:01 AM |
| PHP random quote generator script | grimpirate | PHP | 10 | Sep 24th, 2005 9:22 AM |