samedi 15 décembre 2018

The if/else method doesn't running properly

I want to make a method, which show vbcim as 0 if value is not in the versenyzok.txt, and show the real value if value is found. Something is misunderstood by me, and the Feltolt method doesn't running properly.

It should list the results from the text file. If the 4th value is not found set it as 0, and if its found list it as well

  package vizsgamintaa;
    import java.io.File;
    import java.io.FileNotFoundException;
    //import java.io.PrintStream;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Scanner;
    //import java.util.logging.Level;
    //import java.util.logging.Logger;

    public class VizsgaMintaA {

        static SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
       static Scanner sc= new Scanner(System.in);

        public static void main(String[] args) throws ParseException, FileNotFoundException {
            ArrayList<Versenyzo> versenyzok = new ArrayList<>();
            Feltolt(versenyzok);
            Kiir(versenyzok);
        }
        private static void Feltolt(ArrayList<Versenyzo> versenyzok) throws ParseException{
        Versenyzo v;
        File f = new File("versenyzok.txt");
        try {
                Scanner scan = new Scanner(f, "iso-8859-2");
                while (scan.hasNextLine()) {
                    String sor = scan.nextLine();
                    String[] adatok = sor.split(";");
                    if (adatok.length == 3) {
                       v = new Versenyzo();
                       v.nev = adatok[0];
                        v.szuletes = df.parse(adatok[1]);
                         v.csapat = adatok[2];
                         if (adatok[3].length()==0 )  {

                            v.vbcim = 0; 
                        } 
                         else {
                            v.vbcim = Integer.parseInt(adatok[3]);
                        }
                    } else {
                         v = new Versenyzo (adatok[0],df.parse(adatok[1]),adatok[2],Integer.parseInt(adatok[3]));
                    }
                    versenyzok.add(v);

                    }
            } catch (FileNotFoundException ex) {
                System.out.println("Nincs meg a fájl.");
            }

        }

        public static void Kiir(ArrayList<Versenyzo>versenyzok){
        for (Versenyzo f : versenyzok){
            System.out.println(f.toString());
        }
        }
    }
       class Versenyzo {

           String nev,csapat;
           Date szuletes;
           int vbcim;
           SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");

            @Override
       public String toString(){
       return "Versenyzo:" +nev + "Született:" + df.format(szuletes)+"Csapata:"+ csapat + "Vb címek:" + vbcim; 
       }
       public Versenyzo(String nev, Date szuletes, String csapat, int vbcim) {
           this.nev = nev;
           this.szuletes = szuletes;
           this.csapat = csapat;
           this.vbcim = vbcim;

       }
       public Versenyzo(){
       }
    }

The

else {     
       v = new Versenyzo (adatok[0],df.parse(adatok[1]),adatok[2],Integer.parseInt(adatok[3]));
  }

part is some way not works. ArrayIndexOutOfBoundsException it says.

Aucun commentaire:

Enregistrer un commentaire