• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

is there another way?

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey,
i'm new at this and just want to know "why" it is when i have the instance variable Account aC outside the method, i don't get error info written to my file. also is there a way to return the captured Account from the vector without using this instance variable? the code:

the call to findAccount() is in the main() method as well as the PrintWriter file.println() call. Any salient<reasonable> help or
reference is appreciated. thanks and take care.

n.
[ March 02, 2008: Message edited by: f. nikita thomas ]
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't understand your question completely. Could you let us know the error message you are getting.
Well, I think you could do without creating an Account object in your findAccount() method. I guess, you could simplify your code to something shown below,



hope it helps.
 
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I don't really understand the problem. Have you made those fields and the findAccount static because they are called from the main method? You mention an instance field, but you haven't shown any instance fields; the only field you showed is aX which is static.

Your findAccount method doesn't do that. What it actually does is to open a new Account, then discard it if the Account already exists, the return the old Account. You don't apply a number to the new Account, nor do you add the Account to the Vector.
A better way to find an account would be to declare a local variable aC, set it to null, then iterate through your List, and if you find a matching account, to assign that to the aC variable. Then return aC You need a warning in the documentation comments for that method that it might return null. Any method calling findAccount would then have the option of setting up a new Account if null is returned.

You go on about error messages to a file; I cannot see anything in that code which would raise a runtime error. I am afraid I cannot comment about error messages. Sorry.

BTW: If you have declared Vector<Account> then you probably don't need the class cast. Your Account#getAccount() method appears not to get an Account, but the number. Most people nowadays don't use Vector, but the very similar ArrayList, because of faster performance.
 
Campbell Ritchie
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abdulla Mamuwala, thank you.

It is in fact quite similar to what I suggested.
But a "return" in the middle of a loop breaches the rules of structured programming. There is a lot of controversy about that; you can find out more by a search of these boards for "structured programming."
More seriously, you have forgotten about what happens if no Account is found in the Vector. That would create a compiler error on the grounds of "not every path returns an Account."
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this just gives:

javac dQ.java
dQ.java:17: missing return statement
}
^
1 error

my question is why is it when i include the Account aC variable in the method findAccount(), i'm able to print to my error log, when i declare the variable globally, the file doesn't get written to:


i don't know the etiquette on posting complete source, so until i know i'll just post snippets. thanks.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys,
my "question" is that i "have" a file called "erro.log" that doesn't get written to if i have the variable aC outside the method. could you explain this behavior. would it help if i posted my entire source?
 
Campbell Ritchie
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by f. nikita thomas:
thanks guys,
my "question" is that i "have" a file called "erro.log" that doesn't get written to if i have the variable aC outside the method. could you explain this behavior. would it help if i posted my entire source?

Yes.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the data file:


you're correct in that getAccount() returns the account number. if i run this as is i "won't" get the output to errorlog. i have to run it with the Account aC in the findAccount() method. you mentioned the way to find an account would be to set a variable aC < i assume type Account> to null? this will work?
[ March 02, 2008: Message edited by: f. nikita thomas ]
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell. Yes, the code change I suggested would give an error. The compiler would be unable to see the return statement, and it would complain . I failed to notice this when I suggested the code chnage. Also, as Campbell pointed out this is not the most appropriate and structured way of writing code. Nikita, sorry for the confusion.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no worries Abdulla ;D
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and:


worked fine. didn't know that you could set an object to null. but i still would like to know why i got different effects for the inclusion of the aC variable in the method when it was initialized. thanks again.

n.
[ March 02, 2008: Message edited by: f. nikita thomas ]
 
Campbell Ritchie
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to add a test in the main() method after "else if (r[0].equals("credit")) for null returns. Something like
Otherwise you will get an Exception.

Lose all the instances of "== true". They are all unnecessary.
You appears not to use the int x; local variable in the main method. Lose it.
Change your declaration of Vector<Account> a0 = new Vector<Account>(); to
List<Account> a0 = new ArrayList<Account>();
Change the type of your parameters from Vector<Account> to List<Account> throughout.
Change the addElement() call in main to add().

You need to close the infile Reader as well. You really should have both close() operations in a finally block, but that is too complicated for a late-night posting.

I tried your app, with some sort of guess at what the Account class looks like, and got exactly the same result for erro.log

Invalid account: credit:33-9948568:54

when I had Ac declared as a static field or as a local variable. I can't see why are getting different behaviour in the two cases.

That's all I can do. Good luck with the rest of it.
[edit]Minor spelling correction[/edit]
[ March 02, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . sorry, I hadn't noticed you have already worked out how to handle null returns.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the suggestions, i was just thinking a lot of what i was trying was superfluous. i will implement the ArrayList. my problem was trying to not search repeatedly for an account when it dawned on me:



i now have the index of the Vector captured. i see that ArrayList also has this method. i may be able to complete this by morning. i will take a look at my previous code later to see whether it was logic or language. i'll post my findings. thank you for all your help, Campbell. shalom.

n.
 
Campbell Ritchie
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by f. nikita thomas:
thank you for all your help, Campbell. shalom.

You are welcome.
 
Campbell Ritchie
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but if you have the index captured, you can dispense with the loop. You can even dispense with the return type, and use the index field to find the account. You just have to remember (look in the List, ArrayList and Vector classes in the API) which index you get if you don't find the element. You then use that index as if it were null.
 
I'm still in control here. LOOK at this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic