java


java.util.IllegalFormatConversionException: d != java.lang.String


I have problem when print output list of people by using ArrayList
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));
}
}
Student,Employee, and Customer are sub classes of class Person.When I try to print list,it has an error:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
How can I fix it?
Try this in your code:
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));
use System.out.println(p.get(i).someMethodOrVariableOfPerson); instead of System.out.println(p.get(i));
like below
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);
}
}
you can not directly print Person class object in out.println() method.
out.println() allow only string value

Related Links

Creating a plugin for TeamCity
java xalan transform with and without xinclude in the same app
Error: java.lang.IllegalStateException (EJB3 , jboss7.1)
Why do I get `java.lang.NoClassDefFoundError: scala/Function1` when I run my code in ScalaIDE?
Java Android - Will Decompiling and Compiling end successfully?
Unable to shade jar with maven (INVOKESPECIAL/STATIC)
Exception org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class
java.lang.NullPointerException with facebook onCompleted function
Lotus Notes - Mail Document - Principal/From,INetFrom, SentTime, ReceivedTime fields
How to link the button to fragment class
Hadoop Java vs C/C++ on cpu-intensive tasks
Swapping values in a stream using lambda
how to get value from key in hashmap java
Searching through large int arrays in Java
Integration BUS DFDL as Text Ouput Representation
StringTemplate: how to detect, if variable in tempate is not explicitly set?

Categories

HOME
keras
vmware
bpmn
kalman-filter
setup-deployment
spring-cloud-stream
twitter-bootstrap-4
jpeg
jxls
ios-charts
kibana-4
quicklook
u-sql
visual-studio-2005
ibm-odm
dosgi
telephony
textfield
introduction
opencover
mps
wijmo
sqlcipher
xpages-ssjs
centos6.5
phpfox
fifo
twilio-api
siesta-swift
y86
html5-fullscreen
catalog
revolution-slider
devextreme
reportbuilder
splice
greenrobot-eventbus
preconditions
ensembles
gzipstream
turbogears
netcdf4
executenonquery
scrollable
react-scripts
idisposable
tropo
mplayer
typed-lambda-calculus
capacity
google-cdn
cubes
visual-c++-2008
google-feed-api
ado.net-entity-data-model
database-optimization
qpid
react-native-listview
qgraphicsview
wapiti
natvis
qtableview
endeca-workbench
jmeter-maven-plugin
quicklisp
computer-algebra-systems
ios9.1
key-management
sonarqube5.1.2
csquery
android-nested-fragment
file-copying
picturefill
jboss-weld
mcts
flexmojos
jquery-mobile-dialog
browser-detection
libstdc++
funscript
runas
word-processor
multipage
twrequest
punbb
hgsubversion
firefox-5
microsoft-virtualization
mtj
boost-smart-ptr
project-hosting
scripting-languages
defensive-programming
rtml
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App