?
Solved

Arrays of Strings

Posted on 2002-04-30
5
Medium Priority
?
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

801 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