2016-07-27 17 views
0

Date(int、int、int)コンストラクタを使用しようとしていますが、Date(int、int、int)コンストラクタを使用しようとしています

当初、このコンストラクタは推奨されなくなりました。また、コードの使用に起因してエラーが発生しています。

下記のコードを添付します。私はfileRead.nextInt()を使ってScannerファイルを試してみました。また、以下に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. 
    } 

} 
+1

「4」の前に空白を入力しました。それを解析する前にトリムする必要があります。 – Thilo

+1

あなたが直面する次の問題:廃止されたコンストラクタは1900年と1月にオフセットされた年を0として受け入れます。 – Thilo

+0

'nextInt() 'の問題は何ですか? –

答えて

0

トリム空白

、あなたの数字4の前に出現する空白文字をトリムしなければなりません。文字列でreplace(" " , "")と呼ぶことができます。または、空白を消去するにはGoogle Guavaライブラリを使用します。 utilの

SQLは、日付のみの値を表すことを意図しているjava.sql.Dateクラスに注意してください。これとは対照的に、あなたが使用しているjava.util.Dateクラスは日付と時刻を表します。

日付専用の値の場合は、次に説明するjava.timeフレームワークを使用できない場合は、sql.Dateクラスが適しています。しかし、classが悪いハックであることも知っていますが、util.Dateから継承のその事実を無視し、00:00:00 UTCに調整された埋め込み時刻を無視するように指示しています。混乱している?はい。これらの古い日時クラスは血まみれの混乱です。

java.time

あなたのインストラクターがDateクラスを必要としていると述べていますが、クラスが推奨悪名面倒とではないことを知っている必要があります。

旧式の旧バージョンのクラスjava.util.Dateを使用していますが、これはJava 8以降に組み込まれたjava.timeフレームワークに置き換えられています。

代わりにLocalDateを、時間帯とタイムゾーンのない日付のみの値に使用してください。

LocalDate localDate = LocalDate.of(2016 , 4 , 24); 
関連する問題