-2

The Ujhirdetes method h.hirfel=df.parse(sc.nextLine()); part says: Exception in thread "main" java.text.ParseException: Unparseable date: "" I'm might point to an empty string?

I don't think so because the sample text looks like this: Bekre Pál;110;2018-10-01;42000000 So the text always contains the date.

package vizsgamintab;

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;


public class VizsgaMintaB {
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<Hirdetes> hirdetesek = new ArrayList<>();
       Feltolt(hirdetesek);
       Kiir(hirdetesek);
       Ujhirdetes(hirdetesek);
       Filebair(hirdetesek);
    }

    private static void Feltolt(ArrayList<Hirdetes> hirdetesek) throws ParseException {
        Hirdetes h = null;
        File f = new File ("Lakashirdetes.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){
                h=new Hirdetes();
                h.elado= adatok[0];
                h.alapter= Integer.parseInt(adatok[1]);
                h.hirfel= df.parse(adatok[2]);}
                else if(adatok.length>3) {
                 h = new Hirdetes (adatok[0],Integer.parseInt(adatok[1]),
                         df.parse(adatok[2]),Integer.parseInt(adatok[3]));
                }
                hirdetesek.add(h);
        }}
        catch(FileNotFoundException ex){
                System.out.println("Nincs ilyen file");
                }}
        public static void Kiir(ArrayList<Hirdetes> hirdetesek){
        for ( Hirdetes h: hirdetesek){
            System.out.println(h);
        }
        }
        private static void Ujhirdetes(ArrayList<Hirdetes> hirdetesek) throws ParseException{
        Hirdetes h = new Hirdetes();
            System.out.println("Adjon meg egy új eladót: ");
            h.elado=sc.nextLine();
            System.out.println(" Adja meg a lakás alapterületét:");
            h.alapter=sc.nextInt();
            System.out.println(" Adja meg a hirdetés feltöltésének idejét:");
            h.hirfel=df.parse(sc.nextLine());
            System.out.println(" Adjon meg egy eladási árat:");
            h.ar=sc.nextInt();
            hirdetesek.add(h);

        }
        public static void Filebair(ArrayList<Hirdetes> hirdetesek) throws FileNotFoundException{
        PrintStream f2 = new PrintStream(new File ("Lakashirdetes2.txt"));
        for (Hirdetes h : hirdetesek){
        f2.println(h.toString());

        }
        }

}
    class Hirdetes {
            String elado;
            int alapter, ar ;
            Date hirfel;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");


        @Override
        public String toString(){
        return "Elado neve:" +elado + " Lakás alapterülete:" + alapter+" Hirdetésfeladás:"+df.format(hirfel)  + " Ár:" + ar; 
        }
        public Hirdetes(String elado, int alapter,Date hirfel, int ar){
            this.elado = elado;
            this.alapter = alapter;
            this.hirfel = hirfel;
            this.ar=ar;

        }
    public Hirdetes(){}
    }
Gary
  • 17
  • 4
  • 1
    If the exception states that the string is empty : the string is empty and there is a mistake somewhere ;) Code don't lie – azro Dec 16 '18 at 19:31
  • 1
    use a `System.out.println(Arrays.toString(adatok))` just after creating the array and you'll see what's inside – azro Dec 16 '18 at 19:32
  • It contains 3 value(in some cases 4), and the date is always there in the 3rd place. – Gary Dec 16 '18 at 19:36
  • 1
    Replace all the `sc.nextInt();` with `Integer.parseInt(sc.nextLine())` ;), because your nextLine for date reads the return char of the previous line ;) – azro Dec 16 '18 at 19:43
  • Thanks. That was the sollution. I didn't expect because I used the same pattern for a same problem before,and it worked there. Thanks!! – Gary Dec 16 '18 at 19:49
  • I've written it as answer, so you can accept it and the post will be kind of done ;) – azro Dec 16 '18 at 20:08
  • The classes `Date` and `SimpleDateFormat` are long outdated and the latter in particular notoriously troublesome. Don’t use them in 2018. Instead use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). In particular the `LocalDate` class. Its one-arg `parse` method will parse a string like `2018-10-01` without any explicit formatter. – Ole V.V. Dec 16 '18 at 20:55
  • Welcome to Stack Overflow. For your next questions please indent your code properly so we can read it. It would be even greater if you can [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Ole V.V. Dec 16 '18 at 20:56

1 Answers1

1
  1. Problem

    In the following code :

    h.alapter = sc.nextInt();
    h.hirfel  = df.parse(sc.nextLine());
    
    • the typed int is stored in h.alapter
    • the return line char is comsumed by nextLine() and throws a ParseException
    • the typed date goes no where

  1. Solution

    To fix this prefer use Integer.parseInt(sc.nextLine()), always, you'll avoid a lot of questions and mistakes

    h.alapter = Integer.parseInt(sc.nextLine());
    h.hirfel  = df.parse(sc.nextLine());
    
azro
  • 35,213
  • 7
  • 25
  • 55