java - 日期时间方法和解析不起作用
问题描述
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(){}
}
解决方案
问题
在以下代码中:
h.alapter = sc.nextInt(); h.hirfel = df.parse(sc.nextLine());
- 键入
int
的存储在h.alapter
- 返回行 char 被消耗
nextLine()
并抛出一个ParseException
- 输入的日期无处可去
- 键入
解决方案
要解决此问题
Integer.parseInt(sc.nextLine())
,请始终使用,您将避免很多问题和错误h.alapter = Integer.parseInt(sc.nextLine()); h.hirfel = df.parse(sc.nextLine());
推荐阅读
- ruby-on-rails - Rails ActiveStorage 在没有附加文件时选择随机文件
- android - 我可以从 android studio 在 windows 11 中安装 android 应用程序吗?
- next.js - 在 Vercel 上导入新项目不起作用
- azure-devops - Azure Devops API - 如何按项目名称过滤/搜索?
- javascript - 如何使用 select 或 find 从 BeautifulSoup 访问“父级”?
- android - 如何从片段获取数据到另一个活动?(不是容器活动)
- r - 如何添加一个下载按钮来保存我的绘制数据?
- kubernetes - 无法在 Docker Desktop Kubernetes 下访问本地主机上的应用程序
- css - Safari 上的 Bootstrap 4 Select 不一样
- blazor - BlazorWASM 创建默认用户