Home Help Search Login Register
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome!
 


Pages: [1]   Go Down
  Print  
Author Topic: Java array help  (Read 1600 times)
tootergray34
Newbie
*
Posts: 13


« on: November 09, 2009, 04:27:54 PM »

I'm trying to make this not display multiple times, does anyone know why I have the book being displayed 5 times?
Code:
<html>
<head>
<title>Module 11 Assignment 1</title>
<script language="JavaScript">

var Books= new Array("War and Peace","Huckleberry Finn","The Return of the Native","A Christmas Carol","Exodus");


var Authors = new Array();
Authors[0]="Tolstoy";
Authors[1]="Twain";
Authors[2]="Hardy";
Authors[3]="Dickens";
Authors[4]="Uris";


</script>
</head>

<body>
<script language="Javascript">
document.write("**************************************<br>");

for (i=0; i<Books.length; i++)
{
for(a=0; a<Authors.length; a++)
{
 document.write("<b>Book:</b>", Books[i], "  <b>Author:</b>", Authors[a], "<br>")  
}}

document.write("**************************************<br>");

for (i=0; i<Books.length; i++)
for(a=0; a<Authors.length; a++)
{
 document.write(Authors[a], "   Wrote ", Books[i], "<br>")  
}
document.write("**************************************<br>");


</script>
</html>

Thanks in advance.
« Last Edit: November 10, 2009, 10:25:35 AM by Jenna Hall » Logged
Jenna Hall
Administrator
Full Member
*****
Posts: 101



WWW
« Reply #1 on: November 09, 2009, 09:54:29 PM »

Code:
for (i=0; i<Books.length; i++)
{
for(a=0; a<Authors.length; a++)
{
 document.write("<b>Book:</b>", Books[i], "  <b>Author:</b>", Authors[a], "<br>") 
}}

You're printing out the books 5 * 5 times here because you have 2 loops running, one for Books and one for Authors. Take out the Authors loop and you'll only print your Books 5 times total.
Logged
tootergray34
Newbie
*
Posts: 13


« Reply #2 on: November 09, 2009, 10:02:23 PM »

Oh ok, I see what you are saying, but since I want to have the ouput use both the arrays on the same line of output, could I use this in the FOR loop...:::

for (i=0; i<Books.length; i++ a=0; a<Authors.length; a++)

I tried something similar to this, and It would not let me print out the desired output.

I need to have it look as though:

Book: A Christmas Story Author: Mark Twain
so it would need to have something like this:
 document.write("<b>Book:</b>", Books, "  <b>Author:</b>", Authors[a], "<br>") 

any thoughts?  I love this site, i'm really taking a liking to the C++ tutorial videos as well.
Logged
Jenna Hall
Administrator
Full Member
*****
Posts: 101



WWW
« Reply #3 on: November 09, 2009, 10:13:33 PM »

That for loop is illegal syntax in any language.

You can use the same index for both arrays. So, as long as you can guarantee that your arrays will have the same number of items in them, you can do this:

Code:
for(i = 0; i < Books.length; i++) {
    document.write("<b>Book:</b>", Books[i], "  <b>Author:</b>", Authors[i], "<br>");
}
Logged
Jenna Hall
Administrator
Full Member
*****
Posts: 101



WWW
« Reply #4 on: November 09, 2009, 10:15:28 PM »

Just to be clear for everyone, the language you're writing in is javascript, not Java. But, the for loop logic and index usage is valid in almost every language today.

And thanks--I'm glad you're liking the site!
Logged
tootergray34
Newbie
*
Posts: 13


« Reply #5 on: November 09, 2009, 10:26:19 PM »

Most appreciative for your help and insight, I realized that it was an illegal loop, I didn't know it would work to pull both of them out just for that one line. 

Sorry for sticking this in a Java forum...didn't know where else I could have put my question.

Best,
Todd
Logged
Jenna Hall
Administrator
Full Member
*****
Posts: 101



WWW
« Reply #6 on: November 10, 2009, 10:24:14 AM »

That's what we're here for--to help show you how to do what you want in the language of your choice. Smiley

Not a problem about the java/javascript thing. I may move this to the Development section anyway.

Thanks,
Jenna
Logged
tootergray34
Newbie
*
Posts: 13


« Reply #7 on: November 10, 2009, 02:46:36 PM »

Sorry to keep beating you into the ground with noobish questions.  But suppose I was trying to string out 2 arrays to form a single array.  How would I do that?  here is what I have so far.  I won't copy and paste the whole code...just part of it.

Quote
<body>
<script language="JavaScript">

document.write("**************************************<br>");
for (i=0; i<Books.length; i++) // so long as I have the same index number, I can use for both books and authors since there are 5 items inside each Array.

{
 document.write("<b>Book:</b>", Books, "  <b>Author:</b>", Authors, "<br>") 
}

document.write("**************************************<br>");



var Authors_table=new Array(5)
var a=0;
for (a=0; a<5; a++)
{
   Authors_table[a]=( + Authors.toString() +, ' Wrote ' + Books.toString() + "<br>");
   
}


</script>
</body>

I want to have the Authors_table[a] have 5 seperate lines both with info from array authors and array books.  Does anyone know how to do this?
Logged
Jenna Hall
Administrator
Full Member
*****
Posts: 101



WWW
« Reply #8 on: November 10, 2009, 11:15:57 PM »

You're almost there . . .

Code:
//I don't know the exact syntax to create an array of a certain length in javascript, so look that up
arr_Authors_Table[] = array(Books.length);
for (a=0; a<5; a++)
{
   arr_Authors_Table[a] = Authors[a] + ' Wrote ' + Books[a] + "<br/>";
   
}

You make a new array, arr_Authors_Table, and make it the length of the other array.

Then you loop through the new array, putting a string into each index. You use the same index now for all 3 arrays, which are all the same length.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

XoaX.net Home   |   Reference   |   Play Games!   |   Forum   |   Site Map   |   Contact Us