2

I have a project that I created working perfectly without any errors when doing it at home. However when I import the project to a computer at school, I'm getting two code errors from two different classes.

First Error

Main Class

bank.getAccounts().forEach((i,b)->System.out.println(b));

Illegal start of type ) expected;

Illegal start of expression ; expected;

Second Error

BankProcess Class

bankAccounts.remove(bankAccount.getAccountId(), bankAccount);

No suitable methods found for remove....

I could this be happening? No such errors appear on home computer.

package Coursework1;
import java.util.*;

public class Bank {

  //Creates a new treemap in which Bank Accounts will be stored in.
  private TreeMap < Integer, BankAccount > bankAccounts = new TreeMap < Integer, BankAccount > ();

  //This method returns all bank accounts in the treemap.
  public TreeMap < Integer, BankAccount > getAccounts() {
    return bankAccounts;
  }

  //This method adds a bank account to the treemap.
  public void setAccounts(TreeMap < Integer, BankAccount > accounts) {
    this.bankAccounts = accounts;
  }

  //This method return a bank account using the account number.
  public BankAccount getAccount(Integer accountNumber) {
    return bankAccounts.get(accountNumber);
  }

  //This method removes a bank account from the treemap.
  public void removeAccounts(TreeMap < Integer, BankAccount > accounts) {
    this.bankAccounts = accounts;
  }
}
Alex K
  • 8,269
  • 9
  • 39
  • 57

2 Answers2

4

As far as i know, forEach statement like yours (lambda expression) is not supported in Java 7 and earlier. So my guess is that school computer doesn't have Java 8 installed.

peterremec
  • 488
  • 1
  • 15
  • 46
  • Ok. I understand. I'm using JDK 8 at home but JDK 7 at school. Am I able to change the code so that it works on JDK 7? Does it need a complete makeover? –  Jan 05 '15 at 12:41
  • You just need to change that 'foreach' statement, because lambda expression is not supported in Java 7. Everything else should work fine. If you don't have another Java 8 specific code included, of course... – peterremec Jan 05 '15 at 12:51
  • I dont understand what other expression I can use. I have been learning all my coding through Java 8. How do I change the foreach statement? –  Jan 05 '15 at 12:55
  • Here's an example of using foreach statement in Java 7 and Java 8: http://stackoverflow.com/questions/16635398/java-8-iterable-foreach-vs-foreach-loop If you need additional help, let me know. – peterremec Jan 05 '15 at 13:01
  • Ok. I understand. However I try to implement my coding to it and I'm getting illegal expression errors. for((i,b) : bank.getAccounts) { System.out.println(b); Do I need to change the attributes? –  Jan 05 '15 at 13:08
  • I assume you have some kind of list like: `List accounts = bank.getAccounts();`, right? What exactly does `b` and `i` represent? I believe you should have nested foreach loop like: `for(...){for(...){System.out.println(b);}}` – peterremec Jan 05 '15 at 13:15
  • I have imported the bank class where the list is. –  Jan 05 '15 at 13:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68219/discussion-between-peterremec-and-inside-football). – peterremec Jan 05 '15 at 13:24
  • Try something like that: `TreeMap < Integer, BankAccount > bankAccounts = bank.getAccounts(); for(Entry entry : bankAccounts.entrySet()){ System.out.println(entry.getValue().getAttribute()); }` Just replace `getAttribute()` getter with a getter of the attribute you need to print. – peterremec Jan 05 '15 at 13:31
  • what is Entry suppose to stand for? –  Jan 05 '15 at 13:34
  • Entry is an object, representing TreeMap entry (a , pair). Here's another example of a TreeMap iteration: http://stackoverflow.com/questions/1318980/how-to-iterate-over-a-treemap – peterremec Jan 05 '15 at 13:38
  • Ok. Managed to do it. It function how it should do. reeMap < Integer, BankAccount > bankAccounts = bank.getAccounts(); for(Entry entry : bankAccounts.entrySet()){ System.out.println(entry.getValue().toString()); } –  Jan 05 '15 at 13:39
0

you work with sdk8 at home and there is sdk7 at your school. thats why sdk8 expressions are not supported by sdk7.

Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29