Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Programming Question


  • Please log in to reply
2 replies to this topic

#1 oldezwe

oldezwe

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 25 February 2011 - 08:10 PM

-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
  • 0

#2 oldezwe

oldezwe

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 25 February 2011 - 09:54 PM

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?
  • 0

#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 25 February 2011 - 11:51 PM

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.
  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download