File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array length not declared Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array length not declared" Watch "Array length not declared" New topic
Author

Array length not declared

Srinivas Redd
Greenhorn

Joined: Mar 23, 2006
Posts: 17
Hi ,

Can anybody explain this for me ???


import java.lang.*;

class FirstClass
{

void print()
{
String[] s = new String[];

if(s.length > 0)
System.out.println("Initialsed to some value");
else
System.out.println("Sorry failed");
}


}



class Run
{
public static void main(String argsv[])
{
FirstClass FC = new FirstClass();
FC.print();
}
}


For this code the output is :

C:\Documents and Settings\152749\Desktop\Srinivas\docs\JavaPrograms>javaC RUN.ja
va
RUN.java:8: '{' expected
String[] s = new String[];
^
1 error


Thanks in advance
Srinivas Reddy


Srinvasa Reddy Thamma,<br />Chennai,India.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

First of all, you do not need to import java.lang.*; - the classes in the java.lang package are always imported automatically. So remove the import line.

String[] s = new String[];

You can't create an array without specifying its size. You need to specify the size of the array, for example:

String[] s = new String[10];


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Srinivas Redd
Greenhorn

Joined: Mar 23, 2006
Posts: 17
Hi Thanks,

I know that we have to declare the size, but I want to see what exception we get. I expected that it would ask me to declare size but instead compiler has scold me with someother exception....
C:\Documents and Settings\152749\Desktop\Srinivas\docs\JavaPrograms>javaC RUN.ja
va
RUN.java:8: '{' expected
String[] s = new String[];
^
1 error


Could you explain this ???

Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Because you have not given the array a size, the compiler is expecting you to initialise at the same point you declare it.
e.g.is valid code which will create an array of length 3. So because you haven't initialised it the compiler says that it was expecting a '{';


Joanne
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490


What's wrong with the "if statement"?[/qb]<hr></blockquote>

Nothing is wrong with the if statement, the code originally posted was



As was stated, the error is because the array has no size.
[ April 06, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
Originally posted by Srinivas Redd:
Hi Thanks,

I know that we have to declare the size, but I want to see what exception we get. I expected that it would ask me to declare size but instead compiler has scold me with someother exception....
C:\Documents and Settings\152749\Desktop\Srinivas\docs\JavaPrograms>javaC RUN.ja
va
RUN.java:8: '{' expected
String[] s = new String[];
^
1 error


Could you explain this ???

Thanks



You are expecting error to be reported at runtime while this a syntax error resulting in Compiletime error.


Jass
Srinivas Redd
Greenhorn

Joined: Mar 23, 2006
Posts: 17
Thanks to all...

Srinivas
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
The difference between an array and a list is that

array is of fixed with and
list can grow.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
 
 
subject: Array length not declared
 
Similar Threads
Char with negative showing ?
Double result -- I was surprised
Can there be only one public class in a file
unit testing of singleton classes get methods
Constructors, code won't compile