Results 1 to 15 of 15
  1. #1
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Exclamation [SOLVED] Random number is not working

    Everytime I run this random 4 digit number and 8 digit number outputs as 0. If you could help me and point out what I am doing wrong thatwould be great. Thanks!:)

    import javax.swing.JOptionPane;

    public class generateAccountNumber{
    public static void main(String[] args){


    String bankId = "A4326";
    String result = "";
    final String BANK_ID = "A4326";
    int[]randNums4 = new int[1000];
    int[]randNums8 = new int[1000];
    int i;

    String firstName = JOptionPane.showInputDialog("First Name:");
    firstName.toUpperCase();
    String lastName = JOptionPane.showInputDialog("Last Name:");
    lastName.toUpperCase();

    for(i = 0;i<=randNums4.length && i>999;i++){
    randNums4[i] = (int)(8999.0*Math.random()+1000);
    System.out.println(randNums4[i]);
    }

    for(i = 0;i<=randNums8.length && i>9999999;i++){
    randNums8[i] = (int)(89999999.0*Math.random()+10000000);
    System.out.println(randNums8[i]);
    }
    result += BANK_ID+"-"+lastName.charAt(0)+firstName.charAt(0)+"-"+randNums4[i]+"-"+Character.toUpperCase(lastName.charAt(1))+Charac ter.toUpperCase(firstName.charAt(1))+"-"+randNums8[i];
    System.out.println(result);
    }


    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How many times do those for loops get executed?

    Java Code:
    for(i = 0;i<=randNums4.length && i>999;i++){
        System.out.println("i=" + i);
        randNums4[i] = (int)(8999.0*Math.random()+1000);
        System.out.println(randNums4[i]);
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    @OP, Do you know what's the meaning of this.

    Java Code:
    for(i = 0;i<=randNums4.length && i>999;i++){
    What happen when start to run this line of code?

    I think pbrockway2 try to explain the same thing.

  4. #4
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    How many times do those for loops get executed?

    Java Code:
    for(i = 0;i<=randNums4.length && i>999;i++){
        System.out.println("i=" + i);
        randNums4[i] = (int)(8999.0*Math.random()+1000);
        System.out.println(randNums4[i]);
    }
    It will loop to the length of the array (i.e if the array has 1000 elements it will loop 1000 times) where 'i' starts at 0 but will go straight to 1,000 and beyond
    Last edited by anthonym2121; 04-01-2009 at 07:24 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It will loop to the length of the array (i.e if the array has 1000 elements it will loop 1000 times) where 'i' starts at 0 but will go straight to 1,000 and beyond
    No it doesn't. Try the code I posted (complete with the System.out.println()) and see what it really does.

  6. #6
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    No it doesn't. Try the code I posted (complete with the System.out.println()) and see what it really does.
    It does nothing!

    It just outputs:

    A4326-MA-0-AN-0

    The 0's are the problem

  7. #7
    Natrix is offline Member
    Join Date
    Mar 2009
    Posts
    40
    Rep Power
    0

    Default

    What if you try using Math.random()?

  8. #8
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Natrix View Post
    What if you try using Math.random()?
    I am using Math.random()

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by anthonym2121 View Post
    It does nothing!
    Correct.

    Quote Originally Posted by anthonym2121 View Post
    It just outputs:

    A4326-MA-0-AN-0

    The 0's are the problem
    No, the problem is not those zeros; the problem is that your for loops do nothing. (ie loop precisely zero times).

    I am still not sure what you expected the loop to do. Perhaps you could explain this: is the array expected to grow in size?

    You talk about i "going to straight to 1000 and beyond". But if this really were to happen the best you could expect would be an ArrayIndexOutOfBoundsException because randNums4[i] doesn't exist when i is 1000 or bigger.

  10. #10
    Natrix is offline Member
    Join Date
    Mar 2009
    Posts
    40
    Rep Power
    0

    Default

    Oh sorry didn't see it.

  11. #11
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    for(i = 0;i<=randNums4.length && i>999;i++){
    Java Code:
      for(i = 0;i<=randNums8.length && i>9999999;i++){
    In both the for loops, conditions are not satisfied.
    Let us take first 'for loop'.
    i<=randNums4.length && i>999
    Here randNums4.Length is 1000 and i=0
    But according to your condition, i should be greater than 999 which is not possible.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If you have go through the every post on this thread, pbrockway2 explain the same thing you are talking about.

  13. #13
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    result += BANK_ID+"-"+lastName.charAt(0)+firstName.charAt(0)+"-"+randNums4[i]+"-"+Character.toUpperCase(lastName.charAt(1))+Charac ter.toUpperCase(firstName.charAt(1))+"-"+randNums8[i];
    Try replacing i with i-1 in the above statement

    Java Code:
    result += BANK_ID+"-"+lastName.charAt(0)+firstName.charAt(0)+"-"+randNums4[i-1]+"-"+Character.toUpperCase(lastName.charAt(1))+Charac ter.toUpperCase(firstName.charAt(1))+"-"+randNums8[i-1];

  14. #14
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by kbindumadhavi View Post
    Java Code:
    result += BANK_ID+"-"+lastName.charAt(0)+firstName.charAt(0)+"-"+randNums4[i]+"-"+Character.toUpperCase(lastName.charAt(1))+Charac ter.toUpperCase(firstName.charAt(1))+"-"+randNums8[i];
    Try replacing i with i-1 in the above statement

    Java Code:
    result += BANK_ID+"-"+lastName.charAt(0)+firstName.charAt(0)+"-"+randNums4[i-1]+"-"+Character.toUpperCase(lastName.charAt(1))+Charac ter.toUpperCase(firstName.charAt(1))+"-"+randNums8[i-1];
    Thanks! it works now that I used [i-1]

    Why does [i-1] work but not [i]?

  15. #15
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    for(i = 0;i<randNums4.length;i++)
    In the above for loop, i starts from 0 and increments by 1.
    At at the point i=999, the for loop will be as follows

    i=999
    999<1000(randNums4.length)----> this condition satisfies
    So i increments by 1 and becomes 1000

    Then i=1000
    1000<1000--> this is not satisfied and thus loop ends here.

    So at last value of i is 1000, which started from 0.
    So i=1000 will be out of length. That is if we consider even i=1000, then length becomes 1001 which is not possible
    That is the reason we should replace i-1 with i

Similar Threads

  1. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  2. Random number generater
    By kickflipper1087 in forum New To Java
    Replies: 6
    Last Post: 10-21-2008, 10:19 PM
  3. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 AM
  4. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  5. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 01:42 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
  •