Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error passing values to a method - any help!!

 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently writing a simple test application for a couple of classes. The problem arises when I try to pass values to an add method in the CustomerList class. These values are read from user input as a String and an int value. However, the add method in the CustomerList class is then trying to add these new values to an array within the class.

Basically, how do I send the String and int values to the add method, when the add method is as follows,




My code for the test class is currently as follows,



When I try to run the class I get the following error,



Any help or advice greatly appreciated!!
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the line throwing the compiler error is actually on the screen?
CR
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Many thanks for the reply. Within the main class, the error is being thrown in the following line,



I think the problem is that the method 'add' in the CustomerList class refers to (Customer newItem), whereas the main class reads in both a customer name and number. How do I convert the values read in as a String and an int, in the main class, to a Customer object, which I can then pass to the add method as required in the CustomerList class?

Many thanks!!
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry, I think I have found the line you were referring to.
Find out wherever you are calling that add() method from; there may be several places, and work out what you are passing as a parameter. In one case you would appear to be passing a String rather than what your method requires.
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are passing a String. But your method signature requires a Customer . . .

That is what the error message means.
 
Campbell Ritchie
Sheriff
Posts: 48394
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't mean to pass (new Customer(name, number)), do you???
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That's brilliant. It works now. The best solutions are always the simplest, and usually staring you right in the face. Thank you so much for your help.

One last quick question though. If I read from one file, loans.txt, and then write the data to a data file, loans.data, any idea how I transfer the information from the loans.data file to the loans.txt file when I run the main class?

I know I can just write the new dat straight to the loans.txt file, but I always thought that was particularly bad practice.

Any ideas or help appreciated!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic