Solved

Arrays of Strings

Posted on 2002-04-30
5
137 Views
Last Modified: 2010-03-31
Hi All,
Below is my JSP page:

<html>

<%@page import="java.io.*"%>

<%

String[] sTemp;

sTemp[0] = "Hi";
sTemp[1] = "There";
sTemp[2] = "How";
sTemp[3] = "are";
sTemp[4] = "you";

int i = 0;


for (i=0;i<4;i++)
{
 out.println(i + "<BR>");
  out.println(sTemp[i] + "<BR>");
}

%>

<body>

<h1> Just Java </h1>

</body>
</html>

I have tried using () instead of [], but that does not help. What am I doing wrong?

For some reason, I get this error when running the page in the browser. Jakarta-Tomcat-3.2.3 is my web server on a Windows 2000 OS. IE 5.5 is my browser.

org.apache.jasper.JasperException: Unable to compile class for JSPG:\jakarta-tomcat-3.2.3\work\localhost_8080%2Fjsp-ACO\_0002fjustjava_0002ejspjustjava_jsp_0.java:67: Variable sTemp may not have been initialized.
                sTemp[0] = "Hi";
                ^
1 error

     at org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
     at org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:612)
     at org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)
     at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:542)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:258)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:268)
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
     at org.apache.tomcat.core.Handler.service(Handler.java:287)
     at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
     at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
     at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
     at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
     at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
     at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
     at java.lang.Thread.run(Thread.java:484)
0
Comment
Question by:DoughBoy
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6982141
change String[] sTemp;

to

String[] sTemp = new String[5]; // allocate the memory needed for the array
0
 

Author Comment

by:DoughBoy
ID: 6982254
what if you don't know how big the array will get?
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6982272
In most dynamic cases(thus JSP) there would be a way of finding out the number of spaces needed before setting each value. If not then use an ArrayList instead and change your code.
0
 

Author Comment

by:DoughBoy
ID: 6982336
do you have any sample code to demonstrate your point
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6982428
you could also use a vector:

ie:

Vector v = new Vector();

v.add("Hello");
v.add("World");

for (int i=0;i<v.size();i++) {
   out.println(v.elementAt(i);
}

like ahosang stated, you could also use arraylist.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now