Results 1 to 17 of 17
Thread: acess array list
- 12-09-2010, 03:12 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
acess array list
Hi guys
i have a problem. how i can do a search in a array list?
i need a method that search in arraylist
I have this code
Java Code:private static void up() { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("Your name"); String nconta = sc.next(); //incomplete } //he should access the arraylist with an itenerator public static void ShowAll() { // TODO Auto-generated method stub CurrentAccount conta1 = new CurrentAccount("Alberto Carlos", 1052); CurrentAccount conta2 = new CurrentAccount("Pedro Fonseca", 30); CurrentAccount conta3 = new CurrentAccount("Ricardo Vitor", 1534); CurrentAccount conta4 = new CurrentAccount("João Lopes", 3135); List<CurrentAccount> lista = new ArrayList<CurrentAccount>(); lista.add(conta1); lista.add(conta2); lista.add(conta3); lista.add(conta4); Collections.sort(lista);Java Code:System.out.printf("Bank Accounts:" + "%n"); Iterator<CurrentAccount> itr = lista.iterator(); while (itr.hasNext()) { CurrentAccount element = itr.next(); System.out.printf(element + " " + "%n"); } System.out.println();
if i fallow the same logic (iterator like above) in the method up (), it does a error. I think it is because the arrayList is not being instantiated.
so, the problem is that i can't acess to the arraylist, my ideia is doing a search for an account, if it exists, show respective account in console
i really need help
thanks in advice
- 12-09-2010, 05:35 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
if i fallow the same logic (iterator like above) in the method up (), it does a error
It might be a good idea to post the code you are using and also post the compiler message.
- 12-09-2010, 08:18 AM #3
Could not understand much from what you wrote but...
If you are to search for a CurrentAccount obj.. then why dont use..
Lets be more specific so that guys in here can help you out ...Java Code:lista.contains(currentAccount);
warm regards
Vinod M_______________________________________________
give me beans .........
- 12-09-2010, 03:23 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
my problem is more simple that you think
Java Code:package sistema; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Bank { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int choose; do { System.out.println("Choose an option:\n1 - Withdraw\n2 - Deposit\n3 - Show All Accounts \n4 - Stop "); choose = sc.nextInt(); switch (choose) { case 1: money(); break; case 2: deposit(); break; case 3: ShowAll(); break; case 4: Stop(); break; default: System.out.println("Invalid Key!"); } } while (choose != 4); } private static void money() { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int choose; do { System.out.println("Choose an option:\n1 - Withdraw Current Account\n2 - Withdraw Long Term\n3 - Withdraw Save Money \n4 - Stop "); choose = sc.nextInt(); switch (choose) { case 1: up(); break; case 2: up1(); break; case 3: up2(); break; case 4: Stop(); break; default: System.out.println("Invalid Key!"); } } while (choose != 4); } private static void up2() { // TODO Auto-generated method stub } private static void up1() { // TODO Auto-generated method stub } private static void up() { // TODO Auto-generated method stub Iterator<CurrentAccount> itr = lista.iterator(); while (itr.hasNext()) { CurrentAccount element = itr.next(); System.out.printf(element + " " + "%n"); } System.out.println(); } private static void deposit() { // TODO Auto-generated method stub } private static void Stop() { // TODO Auto-generated method stub System.exit(1); } public static void ShowAll() { // TODO Auto-generated method stub CurrentAccount conta1 = new CurrentAccount("Alberto Carlos", 1052); CurrentAccount conta2 = new CurrentAccount("Pedro Fonseca", 30); CurrentAccount conta3 = new CurrentAccount("Ricardo Vitor", 1534); CurrentAccount conta4 = new CurrentAccount("João Lopes", 3135); List<CurrentAccount> lista = new ArrayList<CurrentAccount>(); lista.add(conta1); lista.add(conta2); lista.add(conta3); lista.add(conta4); Collections.sort(lista); AccountLongTerm conta11 = new AccountLongTerm("Jacinto Alves", 54502); AccountLongTerm conta12 = new AccountLongTerm("Ana Anabela", 30); AccountLongTerm conta13 = new AccountLongTerm("Carlos Brás", 1234); AccountLongTerm conta14 = new AccountLongTerm("José Fonseca", 545); List<AccountLongTerm> lista1 = new ArrayList<AccountLongTerm>(); lista1.add(conta11); lista1.add(conta12); lista1.add(conta13); lista1.add(conta14); Collections.sort(lista1); AccountP conta21 = new AccountP("Manuel Jose", 2200); AccountP conta22 = new AccountP("Carla Costa", 3050); AccountP conta23 = new AccountP("Francisco José", 12214); AccountP conta24 = new AccountP("Alberto Teixeira", 31415); List<AccountP> lista2 = new ArrayList<AccountP>(); lista2.add(conta21); lista2.add(conta22); lista2.add(conta23); lista2.add(conta24); Collections.sort(lista2); System.out.printf("Bank Accounts:" + "%n"); Iterator<CurrentAccount> itr = lista.iterator(); while (itr.hasNext()) { CurrentAccount element = itr.next(); System.out.printf(element + " " + "%n"); } System.out.println(); Iterator<AccountLongTerm> itr1 = lista1.iterator(); while (itr1.hasNext()) { AccountLongTerm element = itr1.next(); System.out.printf(element + " " + "%n"); } System.out.println(); Iterator<AccountP> itr2 = lista2.iterator(); while (itr2.hasNext()) { AccountLongTerm element = itr2.next(); System.out.printf(element + " " + "%n"); } System.out.println(); } }
i have this code, and in method up() e need doing a search in array list-- lista
and i have the error "lista cannot be resolved"
I haven't done the search algorithm, but i want use something like a iteneratorJava Code:private static void up() { // TODO Auto-generated method stub Iterator<CurrentAccount> itr = lista.iterator(); //problem HERE while (itr.hasNext()) { CurrentAccount element = itr.next(); System.out.printf(element + " " + "%n"); } System.out.println(); }Last edited by felito; 12-09-2010 at 03:27 PM.
- 12-09-2010, 06:54 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Java Code:Iterator<CurrentAccount> itr = lista.iterator(); //problem HERE
i have the error "lista cannot be resolved"
You have to understand the notion of scope. You declare the variable lista in the method ShowAll() and it will not be visible or useable after that method has finished.
In general when you declare a variable its scope runs until the closing } of the block where you define it. After that it is said to be "out of scope" and you can't use it.
Java Code:{ // ... int foo; // ... { // ... } // ... } // <-- foo cannot be used past here
This is a real problem because you want to be able to use lista (and possibly the other lists of accounts) in other methods to search for things. Also you want to be able to access list elements (the accounts) so you can process deposits and withdrawls. This will also be a problem if the list goes out of scope.
There are two ways of dealing with this. The first is to move the declaration of lista - and possibly the other lists - and make them class variables.
Java Code:public class Bank { static List<CurrentAccount> lista = new ArrayList<CurrentAccount>(); public static void main(String[] args) { // etc
Now lista will be in scope and useable everywhere in the class.
A much better way is to remove everything that is static except for the static main() method. This is not an option if you are doing an assignment and it says you can't do this. Or if you haven't learnt about constructors. But if it is possible I would advise removing everything static and making lista an ordinary instance variable.
-----------------------
A couple of matters of style and approach:
Method names should start with a lowercase letter. stop() and showAll. Variable and method names should also be descriptive: it doesn't matter what language, but it gets very hard to read when the variables all have similar names.
Work one small step at a time. It is a bit of a distraction having lots of empty methods and do-nothing case statements. These can all be removed until you are ready to write the code that goes there. Compile often and address any problems that the compiler tells you about. It may be - as in this case - that a bit of rewriting and restructuring is needed, but it is always worthwhile. If you can't understand a compiler message, copy and post the whole thing here.
-----------------------
Good luck! and ask if anything here doesn't make sense.
- 12-09-2010, 11:17 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
you are right man!
the problem is solved, but i have another one :p
how i can check if an account exists in arraylist lista for example? with a scanner
the client checks if your account exists in the list.
an iterator? a cycle for?
thanks a lot
- 12-09-2010, 11:30 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
How will the account be identified? By name? You haven't posted the Account classes, but I guess they have something like a getName() method.
If so, I would use a for loop (bucle).
Java Code:/** * Returns a current account matching a given name, or null if no account exists. */ private static CurrentAccount findByName(String name) { for(CurrentAccount ac :lista) { if(ac.getName().equals(name)) { return ac } } return null; } // use it like this CurrentAccount ac = findByName("Pedro Fonseca"); if(ac == null) { System.out.println("Account does not exist"); } else { System.out.println("Found account! " + ac); }
- 12-10-2010, 03:07 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
man thanksssss :)
is almost done! i adapt your code, but strangely i get the message "Account does not exist", but it exists!
the compiler doesn't show any message
thanks :)
Java Code:private static void up() { // TODO Auto-generated method stub CurrentAccount ac = findByName("Pedro Fonseca"); if(ac == null) { System.out.println("Account does not exist"); } else { System.out.println("Found account! " + ac); } } private static CurrentAccount findByName(String name) { // TODO Auto-generated method stub for(CurrentAccount ac :lista) { if(ac.getAccountName().equals(name)) { return ac; } } return null; }
in the future the idea is after checking the account list, the user have the possibility to deposit or withdrawJava Code:package sistema; class CurrentAccount extends Account<Object> { public CurrentAccount(String accountN, double bal) { super(accountN, bal); // TODO Auto-generated constructor stub rate = 0.01; resultLong(); } public String toString() { return "Conta à ordem: " + getAccountName() + ", saldo " + balanceMoney.format(getBalance()) + "€" + ", Juros: " + decimals.format(resultLong) + "€"; } @Override public void deposit(double amount) { // TODO Auto-generated method stub } @Override public double withdraw(double amount) { // TODO Auto-generated method stub if (getBalance()-amount <0){ System.out.println("You don' have suficient founds!"); } else { setBalance(getBalance()-amount); } return getBalance(); } }Last edited by felito; 12-10-2010 at 03:11 AM.
- 12-10-2010, 03:17 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
can you post CurrentAccounts getAccountName() method?
- 12-10-2010, 03:19 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
this is my superclass :)
Java Code:package sistema; import java.text.DecimalFormat; abstract class Account<T> implements java.lang.Comparable<T> { private String accountName; private double balance; protected double resultLong; protected double rate; public Account (String accountN, double bal) { // TODO Auto-generated constructor stub setAccountName(accountN); setBalance(bal); } public int compareTo(Object o) { // TODO Auto-generated method stub if (this.getBalance() > ((Account<?>) o).getBalance()) { return -1; } if (this.getBalance() < ((Account<?>) o).getBalance()) { return 1; } return 0; } DecimalFormat balanceMoney = new DecimalFormat( " 0" ); DecimalFormat decimals = new DecimalFormat( " 0.00 " ); public double resultLong() { return resultLong = balance*rate; } public void setBalance(double balance) { this.balance = balance; } public double getBalance() { return balance; } public void setAccountName(String accountName) { this.accountName = accountName; } public String getAccountName() { return accountName; } public abstract void deposit(double amount); public abstract double withdraw (double amount); }Last edited by felito; 12-10-2010 at 03:23 AM.
- 12-10-2010, 03:26 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
what class is this code from?
can you post it in full?Java Code:private static void up() { // TODO Auto-generated method stub CurrentAccount ac = findByName("Pedro Fonseca"); if(ac == null) { System.out.println("Account does not exist"); } else { System.out.println("Found account! " + ac); } } private static CurrentAccount findByName(String name) { // TODO Auto-generated method stub for(CurrentAccount ac :lista) { if(ac.getAccountName().equals(name)) { return ac; } } return null; }
- 12-10-2010, 03:28 AM #12
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
sorry,
this is the class
Java Code:package sistema; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Bank { static List<CurrentAccount> lista = new ArrayList<CurrentAccount>(); static List<AccountLongTerm> lista1 = new ArrayList<AccountLongTerm>(); static List<AccountP> lista2 = new ArrayList<AccountP>(); public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int choose; do { System.out.println("Choose an option:\n1 - Withdraw\n2 - Deposit\n3 - Show All Accounts \n4 - Stop "); choose = sc.nextInt(); switch (choose) { case 1: money(); break; case 2: deposit(); break; case 3: showAll(); break; case 4: stop(); break; default: System.out.println("Invalid Key!"); } } while (choose != 4); } private static void money() { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int choose; do { System.out.println("Choose an option:\n1 - Withdraw Current Account\n2 - Withdraw Long Term\n3 - Withdraw Save Money \n4 - Stop "); choose = sc.nextInt(); switch (choose) { case 1: up(); break; case 2: up1(); break; case 3: up2(); break; case 4: Stop(); break; default: System.out.println("Invalid Key!"); } } while (choose != 4); } private static void up() { // TODO Auto-generated method stub CurrentAccount ac = findByName("Pedro Fonseca"); if(ac == null) { System.out.println("Account does not exist"); } else { System.out.println("Found account! " + ac); } } private static CurrentAccount findByName(String name) { // TODO Auto-generated method stub for(CurrentAccount ac :lista) { if(ac.getAccountName().equals(name)) { return ac; } } return null; } private static void up1() { // TODO Auto-generated method stub } private static void up2() { // TODO Auto-generated method stub } private static void deposit() { // TODO Auto-generated method stub } private static void stop() { // TODO Auto-generated method stub System.exit(1); } public static void showAll() { // TODO Auto-generated method stub CurrentAccount conta1 = new CurrentAccount("Alberto Carlos", 1052); CurrentAccount conta2 = new CurrentAccount("Pedro Fonseca", 30); CurrentAccount conta3 = new CurrentAccount("Ricardo Vitor", 1534); CurrentAccount conta4 = new CurrentAccount("João Lopes", 3135); lista.add(conta1); lista.add(conta2); lista.add(conta3); lista.add(conta4); Collections.sort(lista); AccountLongTerm conta11 = new AccountLongTerm("Jacinto Alves", 54502); AccountLongTerm conta12 = new AccountLongTerm("Ana Anabela", 30); AccountLongTerm conta13 = new AccountLongTerm("Carlos Brás", 1234); AccountLongTerm conta14 = new AccountLongTerm("José Fonseca", 545); lista1.add(conta11); lista1.add(conta12); lista1.add(conta13); lista1.add(conta14); Collections.sort(lista1); AccountP conta21 = new AccountP("Manuel Jose", 2200); AccountP conta22 = new AccountP("Carla Costa", 3050); AccountP conta23 = new AccountP("Francisco José", 12214); AccountP conta24 = new AccountP("Alberto Teixeira", 31415); lista2.add(conta21); lista2.add(conta22); lista2.add(conta23); lista2.add(conta24); Collections.sort(lista2); System.out.printf("Bank Accounts:" + "%n"); Iterator<CurrentAccount> itr = lista.iterator(); while (itr.hasNext()) { CurrentAccount element = itr.next(); System.out.printf(element + " " + "%n"); } System.out.println(); Iterator<AccountLongTerm> itr1 = lista1.iterator(); while (itr1.hasNext()) { AccountLongTerm element = itr1.next(); System.out.printf(element + " " + "%n"); } System.out.println(); Iterator<AccountP> itr2 = lista2.iterator(); while (itr2.hasNext()) { AccountLongTerm element = itr2.next(); System.out.printf(element + " " + "%n"); } System.out.println(); } }Last edited by felito; 12-10-2010 at 03:31 AM.
- 12-10-2010, 03:32 AM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Seems to me that lista will never have any CurrentAccount object stored unless you call showAll() first.....
- 12-10-2010, 03:37 AM #14
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
you are right
i did the test. First showAll() and after the up() and voila!
is there any alternative? :confused:
i have an alternative. before all, show accounts in consoleLast edited by felito; 12-10-2010 at 03:39 AM.
- 12-10-2010, 03:38 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
adding a bunch of CurrentAccounts to lista before any options are delivered
- 12-10-2010, 03:46 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
It seems to me that showAll() should be for showing the existing accounts. The best place for putting some sample data into the list would be in its own method. (Before the menu options, as already said.)
- 12-10-2010, 03:52 AM #17
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
Reading arrays from different classes without getters and setters
By Psyclone in forum New To JavaReplies: 7Last Post: 02-02-2010, 11:01 AM -
Positions and values, getters, setters
By Malus in forum New To JavaReplies: 10Last Post: 01-23-2010, 05:55 PM -
Getters and Setters
By lheviathan in forum New To JavaReplies: 4Last Post: 11-02-2009, 01:47 AM -
[SOLVED] Problem with program(getters/setters,arrayList).
By Pierced1 in forum New To JavaReplies: 1Last Post: 02-19-2009, 03:50 AM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks