Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Undefined Array Problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Location
    United Kingdom
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Undefined Array Problem

    Hi,
    I'm new to java and have a small but annoying problem with an array. The script rotates a randomly selected banner from an array and changes the banner every few seconds. The problem is that when the page is first loaded the word 'undefined' displays next to the first banner. When the second banner is displayed everything works fine.

    Here is the function I am using

    function randomBanner() 
    { 
     
    var myBanners=new Array(3); 
    var i = Math.floor(Math.random() * 3); 
     
     
    myBanners[0]="image 1"; 
    myBanners[1]="image 2"; 
    myBanners[2]="image 3"; 
     
     
    document.getElementById('banners').innerHTML = myBanners[i]; 
     
    setTimeout("randomBanner()",5000 );//5000 = 5 seconds 
     
    }
    and here is the html code

    HTML Code:
    <head> 
    <script type="text/javascript" src="rotator.js"></script> 
    </head> 
    
    <body> 
    <div id="banners"> 
    <script type="text/javascript">document.write(randomBanner());</script> 
    </div> 
    </body> 
    I have tried declaring the variables and even the array itself outside the function to no avail. I have set up a test page so you can view the problem here

    Please can someone help?
    Last edited by helloworld922; April 13th, 2011 at 08:58 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Undefined Array Problem

    Sorry, but that's javascript code, not Java code. While the two may look similar, they are in fact quite different. I've moved this to the Other Programming Languages forum on the off-chance that someone here knows Javascript, but you may want to consider finding a Javascript forum to ask you question on as they'd be better suited to answer your question.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Undefined Array Problem

    Hello,

    The reason it prints out undefined is because you try to print whatever the randomBanner() method returns which is nothing or undefined.

    You need to change your HTML slightly to this:

    HTML Code:
    <head> 
    <script type="text/javascript" src="rotator.js"></script> 
    </head> 
    
    <body> 
    <div id="banners"> 
    <script type="text/javascript">randomBanner();</script> 
    </div> 
    </body>
    See how the document.write has been removed since we don't need to print the output of the method because the method will change the innerHTML of that div anyway.

    // Json

  4. The Following User Says Thank You to Json For This Useful Post:

    JavaPF (April 18th, 2011)

Similar Threads

  1. array problem
    By aizen92 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 11:06 AM
  2. [SOLVED] array problem
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 12th, 2010, 06:15 PM
  3. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  4. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM