Click to See Complete Forum and Search --> : Stuck trying to output an array value


OctoberWind
02-04-2010, 02:10 PM
First, Java isn't my native language (PHP is, which makes this extra tricky).


<%
String calMonth = dateUtils.getTextMonth(new java.util.Date());
String calNum = dateUtils.getNumberMonth(new java.util.Date());


String calLinkList[];

calLinkList = new String[13];
calLinkList[1] = "http://www.belyea.com";
calLinkList[2] = "http://www.rpmachine.com";
// ... etc.

String calLink = calLinkList[02];
%>

<p>Month: <%=calMonth+calNum %></p>
<p>Link: <%=calLink %></p>



First off, the dateUtils stuff at the top is defined in another location, and pulls Jan, Fab, Mar... Dec and 01, 02,03...12 respectively. I know those work, and get the expected values.

I'm stuck here:

String calLink = calLinkList[02];

I need the "02" to be the calNum variable. i just can't seem to get the variable to plug into the array call. If i use static numbers it works, i just need the static number to be dynamic based on the number from calNum

Month: and Link: at the bottom are my test displays. End result is plugging into an URL.

criterion9
02-04-2010, 02:25 PM
I would use an ArrayList (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html) since it is much more similar to PHP array handling. Then you can use the get() and set() methods to retrieve or store the appropriate values (it handle variables that are int types). Plus the push,pull, etc stack support is much better for ArrayList. You can also iterate through ArrayLists in very similar ways to PHP's foreach(<array> as <variable>) syntax.

To fix your code in the meanwhile Java does not like String indexed Arrays. If you can manipulate your calNum variable to be of type int you should be able to use the variable.


One of the hard changes I had to deal with when switching between PHP and Java is that Java has more strict types where PHP will casually change types as needed.

OctoberWind
02-04-2010, 05:11 PM
Thanks for the tip on ArrayList. I'll look into that.

For the time being, int i = Integer.parseInt(calNum.trim()); seems to do the trick, so I can use String calLink = calLinkList[i]; to grab the correct array value.