正在尝试使用Date(int,int,int)构造函数



我正在尝试使用Date(int,int,int)构造函数(根据讲师的要求),但遇到了一些困难。

最初,我收到了警告,因为显然这个构造函数已被弃用,此外,由于我使用代码,我也收到了错误。

我将在下面附上我的代码。我尝试使用fileRead.nextInt()作为文件扫描程序,我还尝试了下面的Integer.parseInt(fileRead.next()).方法

这是从具有以下格式文本的文件中读取:

firstName lastName, 4, 24, 2016, aStringOfTextPossiblyMultipleWords...

其中4是月,24是日,2016是年。

我得到的错误是。。。

Exception in thread "main" java.lang.NumberFormatException: For input string: " 4"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)
    at java.lang.Integer.parseInt(Integer.java:615)
    at BlogEntryTester.main(BlogEntryTester.java:59)
/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 6 seconds)

这是代码。该错误发生在接近代码末尾的运行时。

import java.util.Date;
import java.util.Scanner;
import java.io.*;
public class BlogEntryTester {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
        Date newDate = new Date();
        BlogEntry BE1 = new BlogEntry();
        BlogEntry BE2 = new BlogEntry("firstName", newDate, "This is the body of "
                + "blog entry number two. This is the last sentence.");
        BlogEntry BE3 = new BlogEntry(BE2);
        BE1.setUsername("randFirstName");
        BE1.setDateOfBlog(newDate);
        BE1.setBlog("This is less than 10 words...");
        System.out.println(BE1.toString());
        System.out.println(BE2.toString());
        System.out.println(BE3.toString());
        Scanner keyboard = new Scanner(System.in);
        Scanner fileRead = null;
        String fileName;
        System.out.print("Enter the name of the file you wish to read from: ");
        fileName = keyboard.next();
        try{
            fileRead = new Scanner(new FileInputStream(fileName));
            System.out.println("> File opened successfully.");
            fileRead.useDelimiter(",|\n");
        }
        catch(FileNotFoundException e){
            System.out.println("> File not found.");
            System.exit(0);
        }
        BlogEntry newBlog = new BlogEntry();
        newBlog.setUsername(fileRead.next()); // Reads username from file.
        if(newBlog.getUsername().length() > 20){
            System.out.println("> Error: Username read from file exceeds 20 "
                    + "characters.");
        }

        newBlog.setDateOfBlog(new Date(Integer.parseInt(fileRead.next()), 
                Integer.parseInt(fileRead.next()), 
                Integer.parseInt(fileRead.next())));
        newBlog.setBlog(fileRead.next()); // Reads the text of the blog.
        System.out.println(newBlog.toString()); // Prints the data gathered from file.
    }
}

修剪空白

正如评论所说,您必须修剪出现在数字4前面的空格字符。您可以在字符串上调用replace( " " , "" )。或者使用Google Guava库来清除空白。

sql与util

请注意java.sql.Date类,它用于表示仅限日期的值。相比之下,您使用的java.util.Date类表示一天中的日期和时间。

对于仅限日期的值,如果不能使用下面描述的java.time框架,则sql.Date类更合适。但也要知道,类是一个糟糕的破解,从util扩展而来。日期,同时指示您忽略继承事实并忽略其嵌入的调整为00:00:00 UTC的时间。令人困惑对这些旧的约会时间课程一团糟。

java.time

你说你的老师要求上Date课,但你应该知道,这门课是出了名的麻烦,不建议上。

您使用的是一个过时的类java.util.Date,它已被java 8及更高版本中内置的java.time框架所取代。

相反,将LocalDate用于不带时间和时区的仅限日期的值。

LocalDate localDate = LocalDate.of( 2016 , 4 , 24 );

最新更新