0

I am trying to use the Date(int, int, int) constructor (per instructor requirements) and I am coming into some difficulty.

Initially I'm getting warnings because apparently this constructor is deprecated and additionally I am getting errors due to my usage of code.

I'll attach my code below. I tried using fileRead.nextInt() for the file Scanner and I also tried the method you see below with Integer.parseInt(fileRead.next()).

This is reading from a file that has text in the format:

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

Where 4 is month, 24 is day, 2016 is year.

The errors I'm getting are...

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)

And here is the code. The error is during runtime near the end of the code.

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.
    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    You entered a space in front of the `" 4"`. That needs to be trimmed before parsing it. – Thilo Jul 27 '16 at 03:13
  • 1
    Next problem you will face: That deprecated constructor accepts years offset by 1900 and January as 0. – Thilo Jul 27 '16 at 03:15
  • What was the problem with `nextInt()`? – OneCricketeer Jul 27 '16 at 03:17
  • @Thilo Thanks! That fixed it! Any easy way to represent the int month in a String now? Where it will say April instead of simply 4? – HotShot51015 Jul 27 '16 at 03:17
  • @cricket_007 nextInt() was giving me similar errors. I'm not quite sure since it should have already been trimming whitespace. – HotShot51015 Jul 27 '16 at 03:18
  • `SimpleDateFormat` or this http://stackoverflow.com/questions/1038570/how-can-i-convert-an-integer-to-localized-month-name-in-java – OneCricketeer Jul 27 '16 at 03:19
  • @HotShot51015 As you found out, the constructor is deprecated (and has been since JDK 1.1 in 1996: that's more than 20 years ago!!!) - there is no valid reason to use it in new code (and if your instructor insists you should probably ask why he wants you to learn obsolete and incorrect techniques). See also: http://stackoverflow.com/questions/5677470/java-why-is-the-date-constructor-deprecated-and-what-do-i-use-instead and http://stackoverflow.com/questions/1999766/the-constructor-date-is-deprecated-what-does-it-mean-java – assylias Jul 27 '16 at 07:24

1 Answers1

0

Trim whitespace

As comments said, you must trim the SPACE character appearing in front of your digit 4. You could call replace( " " , "" ) on the String. Or use the Google Guava library for clearing whitespace.

sql vs util

Be aware of the java.sql.Date class which is intended to represent a date-only value. In contrast the java.util.Date class you are using represents a date plus a time-of-day.

For a date-only value, the sql.Date class is more appropriate if you cannot use java.time framework described next. But also know that class is a bad hack, extending from util.Date while instructing you to ignore that fact of inheritance and to ignore its embedded time-of-day that is adjusted to 00:00:00 UTC. Confusing? Yes. These old date-time classes are a bloody mess.

java.time

You said your instructor is requiring the Date class, but you should know that class is notoriously troublesome and not recommended.

You are using an old outmoded class, java.util.Date, that has been supplanted by the java.time framework built into Java 8 and later.

Instead use LocalDate for a date-only value with no time-of-day and no time zone.

LocalDate localDate = LocalDate.of( 2016 , 4 , 24 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154