0

I tried to rewrite some code, to learn and understand it. I do the same as earlier, but now I got some problem.

List<NewClass> list = new ArrayList<NewClass>();

public static void main(String[] args) {
    try {
        File file = new File("c://data//uzemanyag.txt");
        Scanner szkenner = new Scanner(file, "UTF8");

        while (szkenner.hasNext()){
            String line = szkenner.nextLine();
            String [] c = line.split(";");
            NewClass newclass = new NewClass(Integer.valueOf(c[1]), Integer.valueOf(c[2]));
            list.add(newclass);
        }


    }
    catch (Exception e)  {
        e.printStackTrace();
    }

}

The error is in the "list.add(newclass)" line, but I don't know why, because I already write this part. Here is my another code which is works:

    List<Valtozas> lista = new ArrayList<Valtozas>();

    try {
        File fajl = new File("c://data//uzemanyag.txt");
        Scanner szkenner = new Scanner(fajl, "UTF8");

        while (szkenner.hasNext()) {
            String sor = szkenner.nextLine();
            String [] darabok = sor.split(";");
            String szoveg = darabok[0];
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
            LocalDate ld = LocalDate.parse(darabok[0], formatter);
            //System.out.println("ld: "+ ld);
            Valtozas valtozas = new Valtozas(ld,Integer.parseInt(darabok[1]),Integer.parseInt(darabok[2]));
            lista.add(valtozas);
        }

The only difference, that here I use the Date variable, but now I just want to save the 2 numbers, which contains the txt file.

Cam
  • 61
  • 6

1 Answers1

2

You cannot access non static variables from static content. You may declare your list static

static List<NewClass> list = new ArrayList<NewClass>();

and your program should compile then.

Pavel Niedoba
  • 1,554
  • 2
  • 19
  • 36