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.
}
}
「4」の前に空白を入力しました。それを解析する前にトリムする必要があります。 – Thilo
あなたが直面する次の問題:廃止されたコンストラクタは1900年と1月にオフセットされた年を0として受け入れます。 – Thilo
'nextInt() 'の問題は何ですか? –