• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

error with Strings

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to recheck the syntax of how you create an array.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the lucid explanation. That cleared some of my confusion.
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic