This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

i don't know what i'm doing

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm working on a program that takes a name and phone number, and creates a contact list. When I try and compile this program, I get this error

ContactList.java:57: ')' expected
Contact theContact= new Contact(theList[count]=theContact: name, phone);

and I'm not quite sure what it means.




 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the Javaranch.

Not sure what you're trying to do there. It isn't valid Java syntax. What are you trying to do?
 
Marshal
Posts: 25229
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The colon character in Java denotes the end of a label. Obviously you didn't mean to put a label in the parameters for your Contact constructor, so what's the colon supposed to be for?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt anyone will be able to give you an exact answer, but



calls the constructor for your Contact class, and all of that stuff you have in the argument for your constructor looks unusual, and it's probably wrong.

Give us more code - maybe the signature for your Contact constructor, and we might be able to help.
 
Gwen Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, so I don't know what that was.
what I want to do is make it so the add() will take a name and phone number and add it to theList, which is an instance of Contact()








and thanks for the welcome!
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to look up what an Array is, how to set values, how to retrieve values.

Your teacher needs to look up much more... :(
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And once you have learned how an array works, look at the ArrayList class.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll expand on Mr. Scmidt's comments:

You've defined an Contact array named theList. You state that the function of your method add() is to accept name and phone number arguments and add them to theList. More precisely, I believe you'd like to add the name and phone number to the next available element of your Contact array, theList.

So, to make your add() method work, you'll need to know how large theList array is and how many elements of theList array are already occupied. From that information you'll be able to determine the next index of theList array to which a new member can be assigned.

Once you work out those details, you'll need to know how to assign new elements to the Contact array theList. If you have a chance to discuss that with your instructor, please do. Otherwise, come back here for more help.
 
Gwen Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!

i
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gwen Smith wrote:thanks!

is it possible to use an if statement to check whether an index of an array is null?



Terminology: an index of an array is an integer number that says which element of an array is referred to. If you declare an array of 4 elements, you can refer to the individual elements in that array with the indices (or indexes) 0, 1, 2, and 3.

So an index of an array is an integer number, and it is not possible for an integer to be null. It can be 0, but that is not the same thing.

Now, if you declare an array of objects (such as an array of Contacts), thusly:

Contact contactList = new Contact[4];

That creates an array of these objects. If you were to try to refer to any one of those contact objects just after initializing the array with the above statement, you would discover that all four array elements are, in fact, null. So you could execute the following statement:

if (contactList[0] == null) { System.out.println("Yep, first one ain't here"); }

So, to go beyond this particular question and hook it up with the previous discussion: if you wanted to determine which elements of your array were already in use, you *could* use a loop to look at each element of the array in turn and, when you find one that is null, then you have found a free one that could store another contact.

There are other ways to do it that are better in some dimension, but I am trying to answer the question I think you might have been asking once we get past the teminology issue.

rc
 
Marshal
Posts: 68156
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your constructor as you have written it won't work. You are assigning the value of the parameter to the identifier for the parameter, and the fields are left untouched. You want to try this:The this. tells the compiler to look for the (instance) field of that name.
And a tiny style point: enter single spaces on each side of your binary operators.
 
You got style baby! More than this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!