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
Solved

Arrays of Strings

Posted on 2002-04-30
5
140 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 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