首页 > 解决方案 > 日期时间方法和解析不起作用

问题描述

Ujhirdetes方法h.hirfel=df.parse(sc.nextLine());部分说:我Exception in thread "main" java.text.ParseException: Unparseable date: "" 可能指向一个空字符串?

我不这么认为,因为示例文本如下所示: Bekre Pál;110;2018-10-01;42000000 所以文本总是包含日期。

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(){}
    }

标签: javaparsing

解决方案


  1. 问题

    在以下代码中:

    h.alapter = sc.nextInt();
    h.hirfel  = df.parse(sc.nextLine());
    
    • 键入int的存储在h.alapter
    • 返回行 char 被消耗nextLine()并抛出一个ParseException
    • 输入的日期无处可去

  1. 解决方案

    要解决此问题Integer.parseInt(sc.nextLine()),请始终使用,您将避免很多问题和错误

    h.alapter = Integer.parseInt(sc.nextLine());
    h.hirfel  = df.parse(sc.nextLine());
    

推荐阅读