Jump to content

Java Programming Question

- - - - -

  • Please log in to reply
2 replies to this topic

#1
oldezwe

oldezwe

    Newbie

  • Members
  • Pip
  • 8 posts
-List of names in a database
-names have double assigned to them, x
-each name has random 10 digit double generated from 0 to x
-the name with the highest number double is assigned to a variable
-if two names generate the same double then the name with in the list is assigned to the variable
-once a name is assigned to the variable, it falls out of queue and cannot be assigned again for another 24 hours

What components of Java do I need to be able to understand to know how to do this?

Or if you have pre-written code with similar functionality that would be helpful too.

Thanks tons and much love

#2
oldezwe

oldezwe

    Newbie

  • Members
  • Pip
  • 8 posts
import java.util.Random;

import java.util.Iterator;

import java.util.List;

import java.util.ArrayList;


public final class RandomGaussian {

  

  public static final void main(String... aArgs){

	  Random randomGenerator = new Random();

	  List<Integer> level = new ArrayList<Integer>();

	  level.add(37);

      level.add(2);

      level.add(2);

      level.add(4);

      int position = 0;

      int listsize = level.size();

      while(position <= listsize){

      

      int levelDouble = randomGenerator.nextInt(level.get(position));

      log("Generated : " + levelDouble);

      position = position + 1;

      }

      

      

    

    

    log("Done.");

  }

  

  private static void log(String aMessage){

    System.out.println(aMessage);

  }

}

Why does this give me an array out of bounds error?

#3
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Try changing this:
 int listsize = level.size();
to this:
 int listsize = level.size()-1;
That should work, but Im not sure cause I dont have a Java IDE.
But anyways, your getting an error because an array's value's start at 0. When you call the ".Size" function, it returns the size of the array. So for example you would have an array like this:(pseudo)
ArrayName[0]
ArrayName[1]
ArrayName[2]
ArrayName[3]
Now when you call the ".Size" function it returns 4 because there's four values in the array. But really the answer is 3 because we start holding value's at 0 not 1.
Hope you understand, Im pretty tired lol ~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users