Win a copy of Kotlin Cookbook this week in the Kotlin 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Searching and modifying array list with a method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm not sure that I'm doing this in the most efficient way, but what I'm trying to do is have a method that searches an arraylist which can be called from another class.

I'm getting the error: "symbol accountinfo not found" Line: 39

Note: All imports have been excluded to save space.

Here is the minimal reproducible example:


 
Sheriff
Posts: 14620
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because accountInfo declared on line 15 is local to the accounts() method. That means it exists there only and you can reference it only from lines 15 to 25. Anywhere outside those lines, accountInfo is not accessible.
 
Junilu Lacar
Sheriff
Posts: 14620
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't take this the wrong way but your code shows a gap in your understanding of Java fundamentals and how to use objects.

Here's what I think you're trying to do:

1. Read all the lines of text from a file
2. Search those lines for something

This line is most probably wrong:

The contains(searchQuery) method will search the List for an element, e, where e.equals(searchQuery). I don't know what searchQuery looks like though. Can you give an example of what it might be? Also, what does the text file look like?

 
gary werber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, is there anyway to access the accountinfo from the accountsearcher method?

The text document is a list of bank account names. Bank account names are created via the account name generator, which takes the person's username and password and merges it into an account name.

Ex: username: bob     password: 1234      accountname =  bob1234

So therefore:
If you want to see if an account exists, you type the username and password, the accountname generator turns it into an account name which is our search query. The program then searches the arraylist for the account name (search query)

This section of the program loads the list of account names into an array list. Via methods this arraylist is searched and modified. I want these actions to be performed via a method that can be called
from other parts of the program.

 
Marshal
Posts: 24849
60
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gary werber wrote:Okay, is there anyway to access the accountinfo from the accountsearcher method?



Normally if you want two methods of a class to have access to a variable, you'd make that variable be an instance variable of the class.
 
gary werber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I would declare it at the top of my class?
 
gary werber
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


that solved the issue, hopefully the program runs as I expected.

Thanks guys.
 
Paul Clapham
Marshal
Posts: 24849
60
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the code compiles now? That's great!

However all of those other declarations, I don't see any reason for having them. Basically now the code compiles, you just have the other 90% of the work to do. But hey, you're making progress, drop in again if you have more questions.
 
Marshal
Posts: 67036
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can probably improve the formatting in your code, particularly how to get rid of that very long line 13:-I usually say to avoid static methods, but that method doesn't take any information from the object and doesn't put any information into its object, so it is all right being static.
Don't give it ArrayList as its return type, but List<String>. If you make that change, you can now write the method like this:-Don't return null from anything if you can possibly avoid it. You could return an empty List, but the fact that you have opted to return null suggests to me that you shouldn't catch any exceptions here. Let the calling method handle that exception. Let it supply the file name or Path, which means you can try again and read a different file. So let's change that:-If there is an exception in line 22, line 24 will not be executed and your loop will run again. Put something in the catch to stop the loop, or allow your code to try again.
Remember you can search the List as you would normally, but you might not be able to add or remove elements to it. See readAllLines() method documentation.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!