2017-04-26 83 views
-1

我有問題,當使用ArrayList的java.util.IllegalFormatConversionException:d = java.lang.String中

package data; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 
import java.util.Scanner; 

public class Manager { 
List<Person> p = new ArrayList(); 
Scanner sc = new Scanner(System.in); 

public void addStudent() { 
    String id; 
    String name; 
    int yob; 
    double point1; 
    double point2; 
    System.out.println("Input student id:"); 
    id = sc.nextLine(); 
    System.out.println("Input student name:"); 
    name = sc.nextLine(); 
    System.out.println("Input yob:"); 
    yob = Integer.parseInt(sc.nextLine()); 
    System.out.println("Input point 1:"); 
    point1 = Double.parseDouble(sc.nextLine()); 
    System.out.println("Input point 2:"); 
    point2 = Double.parseDouble(sc.nextLine()); 
    p.add(new Student(id,name,yob,point1,point2)); 
} 
public void addEmployee() { 
    String id; 
    String name; 
    int yob; 
    double salaryRatio; 
    double salary; 
    System.out.println("Input employee id:"); 
    id = sc.nextLine(); 
    System.out.println("Input employee name:"); 
    name = sc.nextLine(); 
    System.out.println("Input employee yob:"); 
    yob = Integer.parseInt(sc.nextLine()); 
    System.out.println("Input salary ratio:"); 
    salaryRatio = Double.parseDouble(sc.nextLine()); 
    System.out.println("Input salary:"); 
    salary = Double.parseDouble(sc.nextLine()); 
    p.add(new Employee(id, name, yob,salaryRatio, salary)); 
} 
public void addCustomer() { 
    String id; 
    String name; 
    int yob; 
    String companyName; 
    double bill; 
    System.out.println("Input customer id:"); 
    id = sc.nextLine(); 
    System.out.println("Input customer name:"); 
    name = sc.nextLine(); 
    System.out.println("Input customer yob:"); 
    yob = Integer.parseInt(sc.nextLine()); 
    System.out.println("Input compnay name:"); 
    companyName = sc.nextLine(); 
    System.out.println("Input bill:"); 
    bill = Double.parseDouble(sc.nextLine()); 
    p.add(new Customer(id,name,yob,companyName,bill)); 
} 
public void addWho() { 
    int choice; 
    do { 
    System.out.println("1.Add Student"); 
    System.out.println("2.Add Employee"); 
    System.out.println("3.Add Customer"); 
    System.out.println("4.Back to menu"); 
    System.out.println("=============="); 
    System.out.println("Choice:"); 
    choice = Integer.parseInt(sc.nextLine()); 
    switch(choice) { 
     case 1: 
      addStudent(); 
      break; 
     case 2: 
      addEmployee(); 
      break; 
     case 3: 
      addCustomer(); 
      break; 
     case 4: 
      break; 
    } 
    } 
    while(choice != 4); 
} 
public void printPersonById() { 
    Collections.sort(p, Comparator.comparing(Person::getId)); 
    for (int i = 0; i < p.size(); i++) { 
     System.out.println(p.get(i)); 
    } 
} 

學生,員工的人打印輸出列表,和客戶是一流的Person.When的子類我嘗試打印列表,他有一個錯誤: 異常線程 「main」 java.util.IllegalFormatConversionException:d = java.lang.String中

我該如何解決呢!?

+1

在哪一行,你得到的異常?請添加完整的堆棧跟蹤 – Jens

+0

這是真的在Android上嗎?使用'System.in'使得這不太可能......請提供[mcve]。 –

+0

你是否重寫'Person','Studend','Employee'或'Customer'中的'toString()'方法? – Hani

回答

0

使用System.out.println(p.get(i).someMethodOrVariableOfPerson);代替System.out.println(p.get(i));

像下面

public void printPersonById() { 
     Collections.sort(p, Comparator.comparing(Person::getId)); 
     for (int i = 0; i < p.size(); i++) { 
      System.out.println("id :"+p.get(i).id); 
      System.out.println("name :"+p.get(i).name); 

     } 
    } 

你不能直接打印Person類對象通過out.println()方法。 通過out.println()只允許字符串值

+0

但我想打印所有人物對象的值,所以我應該使用哪種方法?我編寫了一個類似的應用程序,但只有一個對象,我可以用這種方式打印。 –

+0

讓我看看你的人模特類 –

+0

包數據; public class Person { protected String id; 保護字符串名稱; protected int yob;構造器;吸氣劑和固化劑;的toString; –

0

試試這個在您的代碼:

System.out.println("Input yob:"); 
yob = sc.nextInt(); 
System.out.println("Input point 1:"); point1 =sc.nextDouble(); 
System.out.println("Input point 2:"); point2 = sc.nextDouble(); 
p.add(new Student(id,name,yob,point1,point2)); 
+0

這是一樣的我使用Integer.parseInt(sc.nextLine ))而不是使用sc.nexInt()或sc.nextDouble() –

相關問題