简体   繁体   中英

numberformatexception error on output

I'm getting the following error when compiling and running my program. What did I miss in the formatting?

my csv file looks like the following:

David,Curtis,138,W Plumtree Ln,8012985656 Paul,Taylor,99,Wikapee St,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)

This is my Main

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);

    }

}

This is my class 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 == "8012344567"

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

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

} else {

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

Integer max size is 2,147,483,647, you are trying to parse 8,012,985,656.

Use a Long and Long.valueOf() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM