Results 1 to 8 of 8
Thread: Randomly selected text input
- 11-28-2011, 10:02 PM #1
Randomly selected text input
Hello guys
I am new to the Java scene and would very much like some assistance from you.
I am working in NetBeans and I want to create an array of four chars that later are converted to a string. I have predefined a string, which is a 4-digit pin-code and I want my program to take one character from every inputfield (Using a GUI, and I have 4 passwordfields, I only intend to use 1 digit in each field), turn these four digits into a string and then I plan to use an if statement to check whether they are the same, i.e. password correct or password incorrect.
But I also want the password fields to be RANDOMLY highlighted when the program is initiated, thus making me input (for example) the THIRD char in the array first (although it would still be the third passwordfield)
How to?
- 11-28-2011, 10:21 PM #2
Re: Randomly selected text input
Your requirements are somewhat confusing. I do see the word random several times so you should take a look at the Random class. The best thing to do is try writing some non-GUI code to see if you can get things working like you want. If you have troubles post your code here, include full error messages and ask a specific question.
- 11-28-2011, 10:27 PM #3
Re: Randomly selected text input
Thank you for your reply.
I am using
to get my random numbers from 1-4. Now these 1-4 I would like to assign to each passwordfield that I have created. And when I execute the code, depending on which random number from 1-4 appears, I want the passwordfield assigned to that specific random number to be highlighted and asking me to input a char, and that char would be placed in an array of four chars, in its corresponding place IN the array, to then generate a string that is THEN checked to see whether it is the same as a predefined string that I created.Random rand = new Random();
int pickedNumber = rand.nextInt(5);
Basically, I want to enter an array in a randomly selected order, for example 3,1,4,2. It will not matter which char is entered first because if the third one in the array is entered first, it will find its place, being the third char in the array.
- 11-28-2011, 10:38 PM #4
Re: Randomly selected text input
That code will actually give you random numbers from 0 to 4.
I am still confused. It seems the only randomness is in what order the user enters the chars but what is the point if the still go into their corresponding positions. For eg I enter X into the 3rd field first, A into the 4th field second, W into 1st field 3rd and P into the 2nd second field 4th. That gives me WPXA the exact same word as if I had entered W first, P second, X third, A fourth or P first, A second, W third and X fourth or etc.
If your goal is somehow to randomise the chars then place them into a List and use Collections.shuffle.
- 11-28-2011, 10:45 PM #5
Re: Randomly selected text input
you all
Last edited by AlawiGMX; 11-28-2011 at 11:06 PM.
- 11-28-2011, 10:50 PM #6
Re: Randomly selected text input
Do you not see my point. If all you are going to do is take the first char and make it first, second char second etc then why bother writing code to randomise in what order the user enters the chars?
- 11-28-2011, 10:53 PM #7
Re: Randomly selected text input
suck
Last edited by AlawiGMX; 11-28-2011 at 11:05 PM.
- 11-28-2011, 11:01 PM #8
Re: Randomly selected text input
Personally I think it is a stoopid requirement. However, why not have the textfields in an array, generate your random number, give focus to the textfield in the array at that index, repeat. However you will need to keep track of which random numbers you have generated. Using an earlier suggestion, place your numbers into a List and shuffle it then read out the four numbers one at a time.
Similar Threads
-
Getting input from 3 input text boxes and store into 1 values
By niksipandit in forum New To JavaReplies: 1Last Post: 11-14-2011, 01:03 PM -
Get selected text from MS Word,browser,pdf file
By binhdinh0708 in forum New To JavaReplies: 2Last Post: 04-17-2011, 04:04 PM -
How to obtain text from Selected Menus
By jecarfor in forum AWT / SwingReplies: 1Last Post: 12-03-2010, 05:20 AM -
Displaying text in a JTextArea when a JtextField is Selected
By Frys82 in forum AWT / SwingReplies: 8Last Post: 08-13-2009, 04:23 AM -
Randomly accessing a text file
By bugger in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks