2016-05-08 6 views
1

プログラムのコンパイルと実行中に次のエラーが表示されます。書式設定で私が見逃したのは何ですか?numberformateexception error on output

私のcsvファイルには、次のようになります。

デビッド、カーティス、138、WプラムツリーLnは、8012985656 ポール・テイラー、99、Wikapeeセント、8015984578

Exception in thread "main" java.lang.NumberFormatException: For input string: "8012985656" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at database.DataEntry.createContactInfo(DataEntry.java:76) 
    at database.DataEntry.readcontactlistFromCSV(DataEntry.java:49) 
    at database.DataEntry.main(DataEntry.java:15) 

これは私の主です

package database; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.nio.charset.StandardCharsets; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.List; 

public class DataEntry { 

     public static void main(String...args) { 
      List<ContactInfo> contactlist = readcontactlistFromCSV("C:\\Users\\dcurtis\\Desktop\\DataBaseContactList.csv"); 

      for (ContactInfo c : contactlist) { 
       System.out.println(c); 

     } 
    } 

    private static List<ContactInfo> readcontactlistFromCSV(String DataBaseContactList) { 
      List<ContactInfo> contactlist = new ArrayList<>(); 

      Path pathToFile = Paths.get("C:\\Users\\dcurtis\\Desktop\\DataBaseContactList.csv"); 

      //create an instance of BufferedReader 
      //using try with resource 


      try (BufferedReader br = Files.newBufferedReader(pathToFile, 
        StandardCharsets.US_ASCII)) { 

       //READ THE FIRST line from the text file 

       String line = br.readLine(); 

       //loop until all lines are read 

       while (line != null) { 

        //use string.split to load a string array with the values from 
        //each line of the file 
        //using a comma as the delimiter 

        String[] attributes = line.split(","); 

        ContactInfo contactlist1 = createContactInfo(attributes); 

        // add a contact into ArrayList 

        contactlist.add(contactlist1); 

        //read next line before looping 
        //if end of file reached, line would be null 

        line = br.readLine(); 

       } 

      } catch (IOException ioe) { 
       ioe.printStackTrace(); 

      } 

return contactlist; 

} 

private static ContactInfo createContactInfo(String[] metadata) { 
    String firstName = metadata[0]; 
    String lastName = metadata[1]; 
    int addressNumber = Integer.parseInt(metadata[2]); 
    String addressStreet = metadata[3]; 
    int phoneNumber = Integer.parseInt(metadata[4]); 

    //create and return contact of this metadata 

    return new ContactInfo(firstName, lastName, addressNumber, addressStreet, phoneNumber); 

    } 

} 

これは

私のクラスのContactInfoです
package database; 


import java.util.*; 



public class ContactInfo { 
    Scanner input = new Scanner(System.in); 

private String firstName; 
private String lastName; 
private int addressNumber; 
private String addressStreet; 
private int phoneNumber; 

public ContactInfo(String firstName, String lastName, int addressNumber, String addressStreet, int phoneNumber) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.addressNumber = addressNumber; 
    this.addressStreet = addressStreet; 
    this.phoneNumber = phoneNumber; 

} 
// get the first name when called 
public String getFirstName() { 
return firstName; 

} 

//sets the first name for user or throught the application 
public void setFirstName(String firstName) { 
this.firstName = firstName; 

} 

//gets the last name when called 
public String getLastName() { 
return lastName; 

} 

//sets the last name though the user or application 
public void setLastName(String lastName) { 
this.lastName = lastName; 

} 

// gets the number of the address when called 
public float getAddressNumber() { 
return addressNumber; 
    } 

boolean realNumber; 
    //sets the address number from the user or through the application 

public void setAddressNumber(int addressNumber) { 
//checks to make sure they are only entering intergers 

    if (input.hasNextInt()) { 
     this.addressNumber = addressNumber; 
     realNumber = true; 
    } else { //returns an error message if an invalid number is entered 
     System.out.println("This is not a valid address number. Please eneter again."); 
     realNumber = false; 
     input.next(); 

     } 

} 

// gets the street name 

public String getAddressStreet() { 
return addressStreet; 

} 

//sets the street name by the user or trhough application 

public void setAddressStreet(String addressStreet) { 
this.addressStreet = addressStreet; 

} 

//gets a phone number when called 

public double getPhoneNumber() { 
return phoneNumber; 

} 

//sets a phone number by user or through application 

public void setPhoneNumber(String phoneNumber) { 

//removes any non number and converts to the next comment 

String onlyNums = phoneNumber.replaceAll("[^\\d.]", ""); 

// onlyNums == "8" 

if (onlyNums.length() != 10) { 

System.out.println("Not a vaild number. Please enter 10-digit number."); 

} else { 

this.phoneNumber = Integer.parseInt(onlyNums); 
} 
} 
} 
+2

intの最大値は何ですか?あなたが解析しようとしているものよりも大きいのですか? –

+0

電話番号を「番号」として保存しないでください。理想的には、それらは独自のドメインオブジェクトです。少なくとも、文字列です。 – KevinO

+2

今は、問題を[mcve]に減らすことを練習するのに良い時期です。実際には、問題を示すためにアクティブなコード行が1つ必要です。その値で 'Integer.parseInt'を呼び出すだけです。その後、それを完全なプログラムに入れて、誰かが質問を読むのを容易にして、まだ6行のコードしか得られていません。 –

答えて

0

整数の最大サイズは2,147,483,647で、8,012,985,656を解析しようとしています。

代わりにLongとLong.valueOf()を使用してください。

+0

電話番号が "011 64 3 477 4000"の場合はどうなりますか?先頭の0は問題を引き起こすでしょう。この回答は即時のOP質問を解決するかもしれませんが、長期的には良い提案ではありません。答えはDVではありませんが、このアプローチは一般的には適用できません。 – KevinO