Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2008, 1:05 AM   #11
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

Ok that is what I thought. It's how I implemented my isNumeric() method above. I just wanted to make sure I was reading kewlgeyes's code correctly in that when he runs it and enters a letter his

java Syntax (Toggle Plain Text)
  1. guess = Double.parseDouble(yguess);

will throw an exception if a letter is stored in "yguess". Thanks again for the clarification titaniumdecoy.
manny85 is offline   Reply With Quote
Old Apr 13th, 2008, 10:32 AM   #12
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 80
Rep Power: 7 kewlgeye is on a distinguished road
Smile Re: Random "Letter" Generator

Quote:
Originally Posted by manny85 View Post
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).

I am trying to pull up a window that allows a user to enter a number or a letter between 1-10 or A-J and depending on what they choose, the program will generate a random number or letter depending on the users first input.

The user types A a new window pops up and asks you to guess a letter between A-J and you have 3 trys, if you enter z or a lowercase then you receive an error window.

If the user enters 2 in the first window then you are taken to a new window that asks you to guess a number between 1-10 and you have 3 trys, likewise, if you enter 20 you will receive an invalid number error in a window, otherwise the program terminates with a window that says "Sorry you had three tries, the number or letter was "?" <------- whatever the number or letter was, but it should not display both random number or letter, just the one that you were guessing on.


Thank you all for your help and recommendations on this.

Manny, I tried your program and it works great, it should just show the letter or number though, and the letters are ok, but the numbers are 1 behind, meaning that if the actual number to be guessed is 4 I should enter a 5 in order to get it correct. But way to go, the program itself works. Nice job!
kewlgeye is offline   Reply With Quote
Old Apr 13th, 2008, 10:43 AM   #13
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 80
Rep Power: 7 kewlgeye is on a distinguished road
Re: Random "Letter" Generator

Quote:
Originally Posted by manny85 View Post
Ok that is what I thought. It's how I implemented my isNumeric() method above. I just wanted to make sure I was reading kewlgeyes's code correctly in that when he runs it and enters a letter his

java Syntax (Toggle Plain Text)
  1. guess = Double.parseDouble(yguess);

will throw an exception if a letter is stored in "yguess". Thanks again for the clarification titaniumdecoy.

I was wondering this would make sense, but since I am knew to Java, I hope this won't sound silly. But what if you wrapped if statements around this whole code, and created like I am trying to do but haven't yet, a window that first checks to see if the input is a number or a letter and then goes to the appropriate code which would make the "guess = Double.parseDouble(yguess);" ok

my poor psuedocode

java Syntax (Toggle Plain Text)
  1. if window input == number && !> 10
  2.  
  3. do this
  4. get a random number and give the user three trys to guess it.
  5. after three guesses display a sorry message with the number
  6. else
  7.  
  8. you have entered an invalid number
  9.  
  10.  
  11.  
  12. if window input == A-J && !> J
  13.  
  14. do this
  15. get random number and give the user three tries to guess it.
  16. after three guesses display a sorry message with the letter.
  17.  
  18. else
  19.  
  20. you have entered an invalid character.
kewlgeye is offline   Reply With Quote
Old Apr 13th, 2008, 12:04 PM   #14
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

I believe this pseudo code should work for you. You are on the right track with the input validation (making sure users enter uppercase letters and numbers). I've also added that into the pseudo code so you can have some idea where it could go. I tried to bracket some of the code off to prevent confusion but let me know if you're having issues. If anyone else sees any holes or improvements in the pseudo code feel free to comment.

import java.lang*
import java.util.*;
import javax.swing.JOptionPane;

Declare your variables
Tell user to enter A for random letter or 1 for for random number.
if input = A then
{
	while (attempts < 3 && !done)
	{
	rndGuess = Letter(rndNum (0-9))
	ask user to pick a letter between A-J
	if user input.isUpperCase() then
		if rndGuess.CompareTo(userInputStr) < 0 then
			guessed to low
		else 
		{
			if rndGuess.CompareTo(userInputStr) > 0 then
				guessed too high
			Else
				guessed too correctly
				done = true
		}
		attempts = attempts + 1
	else
		tell user to input only uppercase letters
	if attempts > 3 then
		tell user the correct number
	} // end while
}
else
{
	if input.isNumeric() then
		userInputInt = Integer.parseInt(input)
		if userInputInt == 1 then
		while (attempts < 3 && !done)
		{
			generate rndNum(1-10)
			ask user to pick a number between 1-10
			if user input.isNumeric()
				userInputInt = Integer.parseInt(userInputInt)
				if userInputInt < rndGuess then
					guessed too low.
				else
				{
					if userInputInt > rndGuess then
						guessed to high
					else
						guessed correctly
						done = true
				}
			
				attempts = attempts + 1
			Else
				tell user to enter only numbers.
			if attempts > 3 then
				tell user the correct letter.
		} // end while
	else
		tell user to enter to either A (letters) or 1 (numbers) only
}

http://java.sun.com/j2se/1.3/docs/ap...pperCase(char)
__________________
98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
manny85 is offline   Reply With Quote
Old Apr 13th, 2008, 1:33 PM   #15
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

Quote:
Originally Posted by manny85 View Post
import java.lang*
That line should actually be import java.lang.*;
__________________
98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
manny85 is offline   Reply With Quote
Old Apr 13th, 2008, 2:51 PM   #16
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

java.lang.* is imported automatically in any Java program.
titaniumdecoy is offline   Reply With Quote
Old Apr 13th, 2008, 3:02 PM   #17
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

Well I learn something new everyday Looks like java.lang.* is the only package that is imported automatically according to http://java.sun.com/docs/books/jls/s...doc.html#26741. Thanks again titaniumdecoy, and I hope the links we've provided help you dig into Java's documentation a little easier kewlgeye.
__________________
98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
manny85 is offline   Reply With Quote
Old May 12th, 2008, 1:18 PM   #18
JAlexBrown
Newbie
 
Join Date: May 2008
Posts: 11
Rep Power: 0 JAlexBrown is on a distinguished road
Re: Random "Letter" Generator

Instead of using Double.parseDouble, could you turn guess into an int and Integer.parseInt? That would return a whole number, which could then be found with Letters[guess]. I may be wrong, because I'm still in college learning Java, and I'm sure most of these people are far more experienced than I am, but that's just my two cents.
JAlexBrown is offline   Reply With Quote
Old May 13th, 2008, 9:45 AM   #19
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 8 Fall Back Son is on a distinguished road
Re: Random "Letter" Generator

Maybe I'm going crazy, but wouldn't a much easier way of doing this program be to generate a value that is between whatever the ASCII values for your letters are, then converting it to a char, and printing out the char?
Fall Back Son is offline   Reply With Quote
Old May 13th, 2008, 9:52 AM   #20
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 8 Fall Back Son is on a distinguished road
Re: Random "Letter" Generator

double number = Math.round((Math.random() * 100));
char a = (char)(number);
System.out.println(a);


Yes... if you force it within the correct range, you can generate a letter between A-J only using Math.random() and converting it to char. The code I provided above isn't in the correct range, but it demonstrates what I mean. I hope I'm not overlooking something else in your code that requires the array. But just a thought.
Fall Back Son 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 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




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

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