-1

I'm learning Java and I'm stuck with a little problem. The method .add() applied to my ArrayList object doesn't want to compile. I tried the same thing (with different parameters) on another program and it is working fine, so there's clearly something I've done wrong this time, but I can't figure out what. Any help would be appreciated.

import java.util.List;
import java.util.ArrayList;
private List<String> science = new ArrayList<String>();
science.add("geography");

This bit of code gives me the following error:

  • Syntax error, insert ")" to complete MethodDeclaration
  • Syntax error, insert "SimpleName" to complete QualifiedName
  • Syntax error on token ".", @ expected after this token
  • Syntax error, insert "Identifier (" to complete MethodHeaderName

This is the entire code of the program:

import java.util.ArrayList;
import java.util.List;

public class Facolta {

        private String nome;
        private int facilities, tipo;
        private List<String> science = new ArrayList<String>();
        private List<String> other = new ArrayList<String>();

        science.add("geography");


        public Facolta(String nome, int facilities, int tipo) {
            this.nome = nome;
            if (science.toString().contains(nome)) {
                this.tipo = 2;
            }
            else if (other.toString().contains(nome)) {
                this.tipo = 1;
            }
            this.facilities = facilities;
        }

        public String getNome() {
            return nome;
        }

        public void setNome(String nome) {
            this.nome = nome;
        }

        public void getFacilities() {
            if (tipo == 1)
                System.out.println("Lab number: " + facilities);
            else if (tipo == 2)
                System.out.println("Library number: " + facilities);
            else
                System.err.println("Facility not available.");

        }

        public void setFacilities(int facilities) {
            if (facilities < 3) this.facilities = facilities;
            else System.err.println("Facility not specified.");
        }

}
Claudio Lavacca
  • 31
  • 1
  • 1
  • 5
  • Sangwon Choi's answer is what you need to fix that, but your class has some design flaws: (1) the constructor has the parameter `tipo`, but it's never used. (2) What happen when `name` doesn't match with the contents of `science` or `other`? (3) I can imagine `science` and `other` as types of `Facolta`, so, they should be outside; and so on... Anyway, I guess you're learning, so, keep going :-) – JonDoe297 Dec 21 '16 at 13:12

4 Answers4

3

You can't just put any kind of code in any place of your class.

In this case, you could move that line into your constructor for example.

You could also use an init block; so just put braces

 { 
   sciences.add("blocked");
 }

right there (see here for details on that).

But the real thing is: I guess that science list contains the subjects some student is supposed to take. So it should be rather a parameter of your constructor; instead of hardcoding that string within your source code!

The alternative would be to make that list a real "constant"; like this:

private final List<String> SCIENCES_THAT_GIVE_TIPO =  Arrays.asList("geography", "whatever");

to make your intention clearer!

And beyond that: using Strings to denote such things is of course simple and straight forward; but a rather bad design. So, just in case you want to play around a bit; you might want to read about Java enums; and then consider to turn your different sciences into an Enum!

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

anyway,

List<String> science  = Arrays.asList("geography", "geography2");

This works for your condition

Sangwon Choi
  • 218
  • 2
  • 11
1

The syntax is incorrect.

science.add("geography") - should be done in a method or in a constructor.

J_D
  • 740
  • 8
  • 17
0

I suggestion for ArrayList

ArrayList<String> science =new ArrayList<>(Arrays.asList("geography", "geography2"));
Rince Mathew
  • 27
  • 1
  • 6
  • Can you explain why you would use the specific class type for the variable instead of using the interface type? (and why are you calling it mStatus?) – Scratte Apr 05 '20 at 11:55
  • sorry for the misconception, I thought it was ArrayList. I had the same problem in ArrayList, so I coped it from my code that's why I am calling it as mStatus. – Rince Mathew Apr 05 '20 at 12:42