Results 1 to 17 of 17
  1. #1
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Could not understand much from what you wrote but...
    If you are to search for a CurrentAccount obj.. then why dont use..
    Java Code:
              lista.contains(currentAccount);
    Lets be more specific so that guys in here can help you out ...

    warm regards
    Vinod M
    _______________________________________________
    give me beans .........

  4. #4
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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"

    Java 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();
    		
    		
    	}
    I haven't done the search algorithm, but i want use something like a itenerator
    Last edited by felito; 12-09-2010 at 04:27 PM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  6. #6
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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);
    }

  8. #8
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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;
    	}

    Java 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();
    		}
    
    
    
    		}
    in the future the idea is after checking the account list, the user have the possibility to deposit or withdraw
    Last edited by felito; 12-10-2010 at 04:11 AM.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    can you post CurrentAccounts getAccountName() method?

  10. #10
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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 04:23 AM.

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    what class is this code from?

    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;
    	}
    can you post it in full?

  12. #12
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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 04:31 AM.

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Seems to me that lista will never have any CurrentAccount object stored unless you call showAll() first.....

  14. #14
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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 console
    Last edited by felito; 12-10-2010 at 04:39 AM.

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    adding a bunch of CurrentAccounts to lista before any options are delivered

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.)

  17. #17
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    yes, is the best option

    showAll () before menu


    guys, I am very grateful

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2010, 12:01 PM
  2. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 06:55 PM
  3. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 02:47 AM
  4. Replies: 1
    Last Post: 02-19-2009, 04:50 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •