Adrian Jabko
Adrian Jabko

Reputation: 3

ArrayList how catch exception on one item from constructor

How catch one item from array list and throw exceptions? I try compile this program but is a error. This is contens of message on film.add lines: rok cannot be resolved to a variable.

This is my code. Please for quick answer and thanks

I have two class. class Film and another class

 public class Film {

String tytul;
int rok;
double ocena;
String imierezysera;
String nazwiskorezysera;

public String toString()
{
    return tytul+" "+rok+" "+ocena+" "+imierezysera+" "+nazwiskorezysera;
}


public String gettytul() {
    return tytul;
}
public void settytul(String tytul) {
    this.tytul = tytul;
}
public int getrok() {
    return rok;
}
public void setrok(int rok) {
    this.rok = rok;
}
public double getocena() {

    return ocena;

}
public void setocena(double ocena) {
    this.ocena = ocena;
}
public String getimierezysera() {
    return imierezysera;
}
public void setimierezysera(String imierezysera) {
    this.imierezysera = imierezysera;
}
public String getnazwiskorezysera() {
    return nazwiskorezysera;
}
public void setnazwiskorezysera(String nzawiskorezysera) {
    this.nazwiskorezysera = nzawiskorezysera;
}

public Film(String tytul, int rok, double ocena, String imierezysera, String nazwiskorezysera) {

   this.tytul= tytul;

   this.rok= rok;
    this.ocena= ocena;
    this.imierezysera= imierezysera;
    this.nazwiskorezysera= nazwiskorezysera;
}



public String wyswietl() {
    // TODO Auto-generated method stub
    return tytul+" "+rok+" "+ocena+" "+imierezysera+" "+nazwiskorezysera;
}



}

}

another class

ArrayList<Film> film = new ArrayList<>();
System.out.println("title");
        String tytul;
        tytul = sc.next();
    try{
        System.out.println("year");
        int rok;
        rok = sc.nextInt();
        }catch(Exception e)
        {System.out.println("Wrong value");}



        System.out.println("rating");
        double ocena;
        ocena =sc.nextDouble();

        System.out.println("name");
        String imierezysera;
        imierezysera = sc.next();

        System.out.println("surename);
        String nazwiskorezysera;
        nazwiskorezysera = sc.next();
        film.add(new Film(tytul,rok,ocena,imierezysera,nazwiskorezysera));

this is my code

Upvotes: 0

Views: 630

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533680

rok is defined and only exists inside the scope of the try/catch block.

Note: if nextInt() fails, it doesn't consume the word and your rating could be the invalid year

You could write it like this

String yearStr = sc.next(); // always read the word, even if invalid
int rok = Integer.MIN_VALUE; // value to use if year is invalid.
try {
    rok = Integer.parseInt(yearStr);
} catch (InvalidArgumentException e) {
    System.out.println("Ignoring invalid year " + yearStr);
}

It is usually a bad idea to catch an Exception and then ignore it without check what error occurred. You could get an Exception you don't expect unless you know every possible Exception.

Upvotes: 1

Related Questions