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

Generating id in the low level API
JavaMail getConent after closing folder (folderClosedException)
Running jar with dropped file on bat
java OutOfMemory problem - heap dump 800 Mb smaller than max heap configured
Cross process synchronization in Java
How Password stored in database in encrypted format? [closed]
GWT ERROR: java.lang.RuntimeException: Deferred binding failed for 'com.google.gwt.maps.client.impl.MapImpl'
java tomcat: how to use JOAuth properly
how to i input a sentence so that the program will recognize the sentence as a single word?
Schema Parsing Failed in Solr
log4j: package-specific logging
JTable adding new row
#PostLoad in super class
Desktop Application for Document Generator from Word Template (keeping photos, writing information to documents)
Serializing array types and arrays in Jackson
Is it possible to create an anonymous class while using reflection?

Categories

HOME
testng
netsuite
reserved
fme
vmware
analysis
spring-jdbc
leon
jxls
binary-tree
wamp
fancybox
convolution
uber-api
autotools
append
node-notifier
python-unittest
kibana-4
communication
rascal
quicklook
vb.net-2010
ghc
ef-migrations
serilog
oracle-coherence
xlsxwriter
web-sql
core-text
autosys
emgucv
one-to-many
cas
spark-jobserver
wpfdatagrid
entitlements
buck
opentype
large-file-upload
tasklet
sql-server-2012-express
javascriptcore
asset-pipeline
y86
adobe-premiere
serve
hockeyapp
xenforo
elasticsearch-plugin
dynamic-reports
acoustics
clean-architecture
jxcore
r-forge
qtwebview
mako
mcafee
lync-client-sdk
redundancy
specrun
parallel-data-warehouse
static-ip-address
android-fonts
paypal-express
impresspages
fuzzy-search
ubuntu-10.04
kendonumerictextbox
eclipse-clp
wyam
arcanist
sailfish-os
facebook-graph-api-v2.4
modalpopup
jscript.net
client-side-templating
code-access-security
php-5.4
contenttype
message-driven-bean
jmapviewer
spring-io
ril
gwt-rpc
html4
jquery-mobile-dialog
type-equivalence
smtp-auth
browser-detection
first-responder
labwindows
jspinclude
netdna-api
qt-jambi
horizontal-accordion
sproutcore-2
ext3
microsoft-virtualization
eqatec
sustainable-pace
windows-live-messenger

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