Results 1 to 8 of 8
  1. #1
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Smile 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?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

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

  3. #3
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly selected text input

    Thank you for your reply.

    I am using
    Random rand = new Random();
    int pickedNumber = rand.nextInt(5);
    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.

    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Randomly selected text input

    Quote Originally Posted by AlawiGMX View Post
    to get my random numbers from 1-4.
    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.

  5. #5
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly selected text input

    you all
    Last edited by AlawiGMX; 11-28-2011 at 11:06 PM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default 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?

  7. #7
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Randomly selected text input

    suck
    Last edited by AlawiGMX; 11-28-2011 at 11:05 PM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default 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

  1. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  2. Get selected text from MS Word,browser,pdf file
    By binhdinh0708 in forum New To Java
    Replies: 2
    Last Post: 04-17-2011, 04:04 PM
  3. How to obtain text from Selected Menus
    By jecarfor in forum AWT / Swing
    Replies: 1
    Last Post: 12-03-2010, 05:20 AM
  4. Replies: 8
    Last Post: 08-13-2009, 04:23 AM
  5. Randomly accessing a text file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •