0

I'm getting an error using the method readFile which I used in the class RunProgram.

The error I'm getting is:

RunProgram.java uses unchecked or unsafe operations.

The code of my RunProgram class is:

 import java.util.ArrayList;

 public class RunProgram{        
  public static void main(String [] args){

    String [] namen = Input.geefInputNamen();

    ArrayList<Spelers> spelerDatabase = new ArrayList<Spelers>();
    spelerDatabase = FileReader.readFile("Player Database");    
   }
 }

The method readFile is defined in the code below:

 import java.io.*;
 import java.util.ArrayList;

 public class FileReader {

  public static ArrayList readFile(String naamvdfile) {
    ArrayList<Spelers> spelerDatabase = new ArrayList<Spelers>();   
   try {
int LineNr = 4;         
File aFile = new File (naamvdfile+ ".xlsx");
    BufferedReader aBr = new BufferedReader(new java.io.FileReader(aFile));                    
 String aLine ="";  
    while (aLine !=> null) {
            aLine = aBr.readLine();
            if (aLine != null) {
                String [] lineParts = aLine.split(";", -1);
                if (lineParts.length == 10) {

                    int overall = Integer.parseInt(lineParts[1]);
                    int shootMid = Integer.parseInt(lineParts[4]);
                    int shootDis = Integer.parseInt(lineParts[5]);
                    int defence = Integer.parseInt(lineParts[6]);
                    int rebounden = Integer.parseInt(lineParts[7]);
                    int playmaker = Integer.parseInt(lineParts[8]);
                    int atletisch = Integer.parseInt(lineParts[9]);

                    Spelers speler = new Spelers(lineParts[0], overall, 
                       lineParts[2], lineParts[3], shootMid, shootDis, 
                       defence, rebounden, playmaker, atletisch);
                    spelerDatabase.add(speler);
                }
                else System.out.println("Lijn " + LineNr +" is niet volledig 
                                       ingevuld!");
                LineNr++;
            }       
    }           
  System.out.println("Aantal spelers dat we inlazen: " + LineNr); 
   }        
 catch (Exception e) { }        
 return spelerDatabase;     
    } 
  }

This last code doesn't give an error while compiling.

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158

2 Answers2

0

You get an unchecked warning because readFile() returns an ArrayList while you save it to an object of type ArrayList<Spelers>. Simply change the return type of the method.

public static ArrayList<Spelers> readFile(String naamvdfile) {
    ....
}
Turamarth
  • 2,132
  • 4
  • 25
  • 26
  • i don't get it, do i need to change my return type to ArrayList? Anyway, the things works now :) – Pieter De Smet Nov 21 '18 at 14:34
  • 1
    Correct. The name for what you were using was a "raw type". By omitting the generic type witness you also removed (albeit temporarily) a lot of the type safety surrounding the List. – Rogue Nov 21 '18 at 14:36
0

Change the function return type to ArrayList<Spelers> in the below function in FileReader class :

  public static ArrayList<Spelers> readFile(String naamvdfile) {
    ...

The RunProgram class is expecting an ArrayList<Spelers> whereas the method signature tells it that the return type is an untyped ArrayList.

Pranjal
  • 321
  • 1
  • 5