Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: What is wrong with my ArrayList?

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question What is wrong with my ArrayList?

    Pleas help me!

    I have a problem with my ArrayList, I don't understand how the user input should be stored in the ArrayList and how I can print the ArrayList

    my code:

    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account;
      CommandReader commandReader;
      boolean name;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        commandReader.run();
      }
     
      void enroll(String name){
        if(account.contains(name)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
          account = new ArrayList<Account>();
        }
      }
     
      void deposit(String name, int amount){
        if (account.contains(name)){
          balance = balance + amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void withdraw(String name, int amount){
        if(account.contains(name)){
          balance = balance - amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void print(String name){
        System.out.println("Account of "+name);
        System.out.println("  balance: "+balance);
      }
     
      void printAll(){
        System.out.println(account);
      }
     
      void printRed(){
        if (balance < 0){
          System.out.println(account);
        } else {
          System.out.print("no negative balances");
        }
      }
     
      void interest (double rate){
     
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class CommandReader{
        Bank bank;
        Account account;
     
        /*
        CommandReader(Bank bank) {
            this.bank = bank;                      Why should this be here?
        }*/
     
        //CommandReader() {
        //    bank = new Bank();
        //}
     
        // @pre: input complies to specified format
        void run(){
            Scanner scanner;
            String command;
            String name;
            int amount;
            scanner = new Scanner(System.in);
            bank = new Bank();
            do{
                command = scanner.next().toLowerCase();
                if (command.equals("enroll")) {
                    name = scanner.next();
                    bank.enroll(name);
                } else if (command.equals("deposit")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.deposit(name, amount);
                } else if (command.equals("withdraw")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.withdraw(name, amount);
                } else if (command.equals("print")) {
                    name = scanner.next();
                    bank.print(name);
                } else if (command.equals("printall")) {
                    bank.printAll();
                } else if (command.equals("printred")) {
                    bank.printRed();
                } else if (command.equals("interest")) {
                    double rate = scanner.nextDouble();
                    bank.interest(rate);
                } else if (command.equals("stop")) { 
                    // do nothing
                } else {
                    System.out.println("unknown command");
                    // skip rest of line
                    scanner.nextLine();
                }
            } while (! command.equals("stop"));
        }
    }
     
    class Account{
      double balance;
      String name;
     
      Account(){
        balance = 0;
        this.name = name;
      }
     
      String getName(){
        return this.name;
      }
     
      void setName(String name){
       this.name = name; 
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my ArrayList?

    I would recommend creating a test program: can you write a simple main method that initializes an ArrayList, adds 10 Objects to it, then prints the ArrayList out? Which part of that simple program gives you trouble? Post it here in the form of an SSCCE, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my ArrayList?

    My program looks now like this:

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account = new ArrayList<Account>();
      CommandReader commandReader;
      boolean name;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        commandReader.run();
      }
     
      void enroll(String name){
        if(account.contains(name)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
        }
      }
     
      void deposit(String name, int amount){
        if(account.contains(name)){
          balance = balance + amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void withdraw(String name, int amount){
        if(account.contains(name)){
          balance = balance - amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void print(String name){
        System.out.println("Account of "+name);
        System.out.println("  balance: "+balance);
      }
     
      void printAll(){
        System.out.println(account);
      }
     
      void printRed(){
        if (balance < 0){
          System.out.println(account);
        } else {
          System.out.print("no negative balances");
        }
      }
     
      void interest (double rate){
     
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class CommandReader{
        Bank bank;
        Account account;
     
        /*
        CommandReader(Bank bank) {
            this.bank = bank;
        }*/
     
        //CommandReader() {
        //    bank = new Bank();
        //}
     
        // @pre: input complies to specified format
        void run(){
            Scanner scanner;
            String command;
            String name;
            int amount;
            scanner = new Scanner(System.in);
            bank = new Bank();
            do{
                command = scanner.next().toLowerCase();
                if (command.equals("enroll")) {
                    name = scanner.next();
                    bank.enroll(name);
                } else if (command.equals("deposit")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.deposit(name, amount);
                } else if (command.equals("withdraw")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.withdraw(name, amount);
                } else if (command.equals("print")) {
                    name = scanner.next();
                    bank.print(name);
                } else if (command.equals("printall")) {
                    bank.printAll();
                } else if (command.equals("printred")) {
                    bank.printRed();
                } else if (command.equals("interest")) {
                    double rate = scanner.nextDouble();
                    bank.interest(rate);
                } else if (command.equals("stop")) { 
                    // do nothing
                } else {
                    System.out.println("unknown command");
                    // skip rest of line
                    scanner.nextLine();
                }
            } while (! command.equals("stop"));
        }
    }
     
    class Account{
      double balance;
      String name;
     
      Account(){
        balance = 0;
        this.name = name;
      }
     
      String getName(){
        return this.name;
      }
     
      void setName(String name){
       this.name = name; 
      }
    }

    But still it gives not the right outcome.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my ArrayList?

    I would recommend creating a test program: can you write a simple main method that initializes an ArrayList, adds 10 Objects to it, then prints the ArrayList out? Which part of that simple program gives you trouble? Post it here in the form of an SSCCE, and we'll go from there.

    --- Update ---

    Your ArrayList holds accounts. Why are you passing a String into the contains() method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my ArrayList?

    [/COLOR]Your ArrayList holds accounts. Why are you passing a String into the contains() method?[/QUOTE]

    I've put name in it, because when the user types a command like enroll "name" it should only make a new account when there is no account with the same name in the ArrayList?

    --- Update ---

    Quote Originally Posted by KevinWorkman View Post
    I would recommend creating a test program: can you write a simple main method that initializes an ArrayList, adds 10 Objects to it, then prints the ArrayList out? Which part of that simple program gives you trouble? Post it here in the form of an SSCCE, and we'll go from there.

    --- Update ---

    Your ArrayList holds accounts. Why are you passing a String into the contains() method?
    I've put name in it, because when the user types a command like enroll "name" it should only make a new account when there is no account with the same name in the ArrayList?

    import java.util.Scanner;
    import java.util.ArrayList;
     
    class BankTest{
      ArrayList<Account>account = new ArrayList<Account>();
      Scanner sc = new Scanner(System.in);
      String command, name;
     
      void run(){
        command=sc.next().toLowerCase();
        if (command.equals("enroll")){
            name=sc.next();
            enroll(name);         
        }
      }
     
      void enroll(String name){
        if(account.contains(name)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
        }
      }
     
      void printAll(){
        /*if(account.contains()){
          System.out.println(account);
        }*/
        System.out.println(account);
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class Account{
      double balance;
      String name;
     
      Account(){
        balance = 0;
        this.name = name;
      }
     
      String getName(){
        return this.name;
      }
     
      void setName(String name){
       this.name = name; 
      }
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my ArrayList?

    You're only ever adding in Accounts to your ArrayList. It will never contain a String.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my ArrayList?

    Quote Originally Posted by KevinWorkman View Post
    You're only ever adding in Accounts to your ArrayList. It will never contain a String.
    I don't know what you mean. Could you explain it with an example? I'm not so familiair with english terms, because I'm from Holland.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my ArrayList?

    Your ArrayList only holds Accounts:

    ArrayList<Account>account = new ArrayList<Account>();

    You never add any Strings to that ArrayList. In fact, that would cause a compiler error. Yet you check whether the ArrayList holds a particular String, which it never will:

    void enroll(String name){
        if(account.contains(name)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
        }
      }

    So that if statement will never evaluate to true. You need to rethink what you're doing here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my ArrayList?

    Quote Originally Posted by KevinWorkman View Post
    Your ArrayList only holds Accounts:

    ArrayList<Account>account = new ArrayList<Account>();

    You never add any Strings to that ArrayList. In fact, that would cause a compiler error. Yet you check whether the ArrayList holds a particular String, which it never will:

    void enroll(String name){
        if(account.contains(name)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
        }
      }

    So that if statement will never evaluate to true. You need to rethink what you're doing here.

    I understand what you say, but I just don't know how it works, could you give me a hint?

    what I have right now:

    void enroll(String name){
        if(account.equals(account)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
        }
      }

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is wrong with my ArrayList?

    You need to think about what exactly you're trying to do.

    Write a test program that programatically adds a few Accounts with hardcoded names to an ArrayList of Accounts. Then write a function that tests whether an Account with a certain name is in the ArrayList.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What is Wrong When calling the function using arrayList.
    By nutan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2013, 07:30 AM
  2. user input going to wrong ArrayList
    By havinFun in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2012, 02:58 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM