Ich bekomme den folgenden Fehler beim Kompilieren und Ausführen meines Programms. Was habe ich bei der Formatierung vermisst?numberformatexception Fehler bei der Ausgabe
meine CSV-Datei sieht wie folgt aus:
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)
Das ist mein Haupt
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);
}
}
Das ist meine Klasse Contact
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);
}
}
}
Was ist der größte Wert für ein int? Ist es größer als das, das du parsen willst? –
Speichern Sie Telefonnummern niemals als "Nummer". Idealerweise sind sie ihr eigenes Domänenobjekt. Zumindest sind sie ein String. – KevinO
Jetzt wäre es auch eine gute Zeit, um Probleme zu reduzieren auf eine [mcve] - wirklich brauchen Sie nur eine aktive Zeile Code, um das Problem zu demonstrieren ... aufrufen "Integer.parseInt" mit diesem Wert. Dann fügen Sie das in ein vollständiges Programm ein, um es für jeden einfacher zu machen, die Frage zu lesen, und Sie haben immer noch nur 6 Zeilen Code ... –