aspose file tools*
The moose likes Beginning Java and the fly likes error with Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error with Strings" Watch "error with Strings" New topic
Author

error with Strings

Kashyap Hosdurga
Ranch Hand

Joined: Sep 19, 2005
Posts: 83
I have a class and in the constructor I am constructin a string array.
Class X {
String TaxTypes[];
X(){
new TaxTypes[16] = {"FED","state_1","city_1","tax1","tax2","tax3","tax4","tax5","tax6","tax7","tax8","tax9","tax10","tax11","tax12","tax13"};
}

I am getting the following error (pointer near tax11)

illegal start of expression
new TaxTypes[17] = {"FED","state_1","city_1","tax1","tax2","tax3","tax4","tax5","tax6","tax7","tax8","tax9","tax10","tax11","tax12","tax13"};
^
Can anyone please help me to resolve this issue.

Thanks,
Kashyap
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You need to recheck the syntax of how you create an array.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Hmmm... There is actually no way to give a hint here. Either you know the correct syntax, or you don't. Anyway, here is the correct syntax...



I suggest that you review arrays to figure out why.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A suggestion...

When declaring an array, the identifier (variable name) can appear either before or after the brackets. That is, you can either say String taxTypes[] (as you've done), or you could say String[] taxTypes.

I suggest that placing the brackets next to the element type is more clear, because the object is actually an array of String references (String[]) -- not an array of taxTypes references (taxTypes[]).

You might also avoid confusion by noting that identifiers conventionally begin with a lowercase letter (e.g., taxTypes), while types begin with an uppercase letter (e.g., String).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

On the syntax...

As objects, arrays must be declared and instantiated. The size of the array must be established at instantiation. This can be specified with a number inside the brackets, as follows:

String[] theseStrings = new String[3];

In this situation, the 3 String references are initialized to null. However, array elements can also be specified at the point of instantiation, as follows:

String[] theseStrings = new String[] {"abc", "def", "ghi"};

And this can be expressed in shorthand as...

String[] theseStrings = {"abc", "def", "ghi"};

So the size of the array is indicated either explicitly with a number in the brackets, or implicitly with a specific quantity of elements within the braces. But in both cases, this is on the right side of the assignment. Also, a compile-time error will result if the size is specified both explicitly and implicitly.

//ILLEGAL...
String[] s = new String[2] {"ab", "cd"};
[ May 12, 2006: Message edited by: marc weber ]
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Thanks for the lucid explanation. That cleared some of my confusion.


SCJP 1.4 85%
Kashyap Hosdurga
Ranch Hand

Joined: Sep 19, 2005
Posts: 83
Hi all,

Thanks for the reply.
I had to declare and instantiate the array separately.

class Test {
public String[] taxTypes;
Test() {
String[] taxTypes = {"Fed", "state","city","tax1","tax2","tax3","tax4","tax5","tax6","tax7","tax8","tax9","tax10","tax11","tax12","tax13"};
}
public static void main(String args[]) {
Test obj1 = new Test();
}
}

Thanks for all the replies.

Kashyap
Kashyap Hosdurga
Ranch Hand

Joined: Sep 19, 2005
Posts: 83
Sorry about the confusion,

I need to declare an array in the class and construct it in a constructor.
But I am confused about doing it. Any help is appriciated.

class test {
String[] testStringArray;
test() {
testStringArray[] = {"One", "Two", "Three", "Four"};
}


But I am getting error if I use the above syntax.

Thanks,
Kashyap
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10



This is wrong because in the constructor you are creating a new, local variable that hides the member variable and you're initializing that, instead of the member variable.



The variable is called testStringArray, not testStringArray[]. Remove the brackets.

Do you have a book to learn Java from? Read the book...
[ May 15, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error with Strings
 
Similar Threads
HTTP response
((A)b).x what kind of construct is this?
Inheritance question
implementing compareTo() - doubt
int to string conversion using toStirng()