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

Calling A Method

 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project that is giving me trouble. I have read up on calling static methods but this case will not let me call the method I want to call. please help. Keep in mind I'm a beginner and the method code is not finished.

// This case adds a new sales rep to the sales force.

case ADD_REP: {
System.out.print("Enter New Sales Representative: ");
String name = Utility.readString();
if (name = findRep(name)) {
System.out.println("New Sales Representative entered: " + name);
}
else {
System.out.println("Representative allready exists.");
}
System.out.println("");
Utility.pressEnter();
break;
}

public static SalesRep findRep(String name) {
for (int i = 0; i < reps.size(); i++) {
if (reps.equals(name)) {
return findRep(name);
}

}return null);
}
[ November 09, 2005: Message edited by: Ryan Newcombe ]
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was indented when posted it.
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you use the code tags it will preserve indentation...


Now onto the problem. What is the first part of the code.. the case statement. Do you have that inside another method (and inside a switch statement) or does it actually look like that? Because it needs to be inside something and probably a method at that.

It would probably be good if you told us what errors the compiler is complaining about. It will tell you the line number so if you told us that it would help. (Don't tell us the line number just tell us what line of what you posted is giving you grief)
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The case is part a switch with several choices. It is not inside a method. I have changed a couple things since the first post but I know I am still wrong. The error is about the same every time "Cannot find symbol: addRep() location line 68". Why won't it find and recognize the method?

code:
--------------------------------------------------------------------------------


switch (choice) {

// This case adds a new sales rep to the sales force.

case ADD_REP: {
System.out.print("Enter New Sales Representative: ");
String name = Utility.readString();
if (name == (name)) {

System.out.println("New Sales Representative entered: " + (addRep()));
}
else {
System.out.println("Representative allready exists.");
}

System.out.println("");
Utility.pressEnter();
break;
}
// This method can be called to add a SalesRep object to the end of
// the ArrayList. The SalesRep object is received as a parameter. If
// the list doesn't already contain the object, it is added to the
// list and true is returned to indicate success. Otherwise, false
// is returned to indicate the existence of a duplicate rep.

public static boolean addRep(SalesRep rep) {
for (int i = 0; i < reps.size(); i++) {
if (reps.equals(rep)) {
return addRep(rep);
}
else {
return false;
}
}return addRep(rep);
}
[code]
[ November 09, 2005: Message edited by: Ryan Newcombe ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The switch, like all other Java statements must occur inside of a method or constructor (or an initialization block). Perhaps you want this to go inside main(), or maybe somewhere else?

In any case, the compiler sees just a shambles, so the error messages don't really mean much of anything. Put that code inside of a method, and things will start to make more sense.
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I didn't realize that the main method....is a method...seems simple now. OK the switch is in the main method and so are the methods I'm trying to call.

public class Project02 {

public static void main(String[] args) {
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Let's see what else we can find. This line

if (name = findRep(name)) {

has a number of problems. First, the "=" operator is an assignment operator; you probably want to compare things using the "==" operator.

if (name == findRep(name)) {

Except name is a String, and findRep() returns a SalesRep, so that can't be right: a String and a SalesRep will never be equal. I think, looking at the findRep() method, that you actually mean "if findRep() returns non-null", which would be

if (findRep(name) != null) {

which is good.

Now, in the findRep() method, you're searching the collection "reps" for a given SalesRep object. First, in the line

if (reps.equals(name)) {

you're asking if the whole collection is equal to the name; that's surely not right. A little bit closer would be to compare the i-th element of the collection to name:

if (reps.get(i).equals(name)) {

but it looks as though the collection contains SalesRep objects, not String, so again, this isn't going to work. Maybe you want something like

if (((SalesRep) reps.get(i)).getName().equals(name)) {

Now, on the next line you cal findRep(name) recursively (you call a method from itself) which is almost certainly not what you want here. I think you just want

return (SalesRep) reps.get(i);

Finally, there's an extra close-parenthesis after "return null".

Undoubtedly there are more issues -- but this should get you much closer!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic