Admittedly I am only a beginner at playing with java but I'm pulling my hair out trying to understand something I thought would be easy, and it probably is. Trying to write code that randomly selects from 3 preloaded arrays and spits out the results. Mechanically the code seems to work fine but I'd like the result in a different format than this code gives. Currently the 3 individual selections are displayed in input boxes.

var girl = new Array("Michelle","Sandy","Mary")
var place = new Array("dinner","movies","beach")
var day = new Array("Friday","Saturday","Sunday")
var suggestion
var i=0
function PickDate()
	i=Math.floor(Math.random() * girl.length);
	i=Math.floor(Math.random() * place.length);
	i=Math.floor(Math.random() * day.length);
<h2>Date Picker</h2>
<FORM NAME="DatePick">
<INPUT TYPE="BUTTON" NAME="Button1" VALUE="Generate Your Date!" ONCLICK="PickDate()">
document.write ("Take "GIRL" to the "PLACE" on "DAY);

I want to do away with the 3 input boxes and have the result printed out as a single line with the individual selections embedded in a sentance similar to the doc.write command above
ie: Take Michelle to the movies on Friday.


5 Years
Discussion Span
Last Post by javaAddict

I am sorry to tell you this, but if you are a beginer with java why do you write jsps? The code that you wrote has several errors, but not because of your lack of effort. Because there some things that you have confused and you need some studying to do first before start writing. The code will not work the way you "imagine" that it will work. You write html and javascript and assume that it will behave like java does:

That is not how you declare variables in html and you cannot reference it like this:
document.write ("Take "GIRL" to the "PLACE" on "DAY);
because there aren't any variables in html that you can call at "runtime"

The function PickDate is written in javascript and you are trying to call java methods in it.

You are trying to write html as if you are writing java in a main method.

From your code one can understands that you have confused a lot of things about the differences between java - html - jsp - javascript and it will not help you learn if someone just writes it for you.

As hiddepolen stated - while I was preparing this post - what you are trying to do can be done with pure javascript. You need to declare the html tags correctly and use the apropriate javascript in the PickDate. You are also trying to use java in the html withour java scriplets.

In general you should stick with java for a while. What book did you read before writing this code. You need first to learn java well. Not jump into html like that. Then learn html, write a lot of examples with only html inside and only then you should start reading a book about jsps and servlets. You don't need to add javascript in the mix yet. Do you have any teacher or anyone else that can teach that?

For me this site helped me learn html:

It shouldn't take you more than a few days to look through the html tutorial and practice with the examples.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.