Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array length not declared

 
Srinivas Redd
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
 
Srinivas Redd
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 '{';
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all...

Srinivas
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between an array and a list is that

array is of fixed with and
list can grow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic