Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error with Strings

 
Kashyap Hosdurga
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to recheck the syntax of how you create an array.
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the lucid explanation. That cleared some of my confusion.
 
Kashyap Hosdurga
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15282
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic