Oliver Oliver - 4 months ago 22
Java Question

exception in thread main java.util.illegalformatconversionexception d = java.lang.double

Java newbie here, I've been making a program that calculates which option during bus ride is better: single card, month card or a day card. So I get this error:

exception in thread main java.util.illegalformatconversionexception d = java.lang.double


It has something to do with
d
being a
double
I'm guessing. But
double
should be fine.

Another question is- how do I make the program choose from 2 random cheaper ways if 2 or 3 options are all same cheap?

code:

import java.util.Scanner;

public class kt_3_3
{

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter amount of days:");
double p = scan.nextDouble();

System.out.println("Enter amount of rides:");
double s = scan.nextDouble();

System.out.println("Enter the price of one time ticket:");
double y = scan.nextDouble();

System.out.println("Enter the price of a day ticket:");
double d = scan.nextDouble();

System.out.println("Enter the price of a month card:");
double k = scan.nextDouble();

if ((y * s) > (p * d)) {

System.out.printf("Cheaper option is single cards");

} else if ((p * d) > (y * s)) {

System.out.printf("Cheaper option is %f day tickets", p);

} else if ((k > (y * s))) {

System.out.printf("Cheaper option is a month card");

} else if ((p * d) > k) {

System.out.printf("Cheaper option is %f day tickets", p);

} else if ((y * s) > k) {

System.out.printf("Cheaper option is single cards");

} else if (k > (p * d)) {

System.out.printf("Enter the price of a month card:");

} else if ((k == (p * d) == (y * s))) { //this doesnt seem to be working

System.out.printf(""Cheaper option is single cards"")

}

}

}


I appreciate the help!
Edit: (solved by myself, simply forgot to put the else if statement between additional () ) ive spotted another flaw, and that is- if calculates 2 but doesnt take the 3rd one into consideration. For expample: if y * s < p * d but really "k" is the cheapest, it will say that y * s is cheapest. I've tried to use the && to format a 2nd statement there aswell but it throws me "else" without "if", " ; expected" and illegal start of expression.

Edit 2: New code, completly working :

import java.util.Scanner;

public class kt_3_3 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter days:");
double p = scan.nextDouble();

System.out.println("Enter amount of drives:");
double s = scan.nextDouble();

System.out.println("Enter one time ticket cost:");
double y = scan.nextDouble();

System.out.println("Enter the cost of a day ticket:");
double d = scan.nextDouble();

System.out.println("Enter the price of a month card:");
double k = scan.nextDouble();

if (((y * s) < (p * d)) && (y * s) < k) {

System.out.printf("Cheaper way is one time tickets");

} else if (((p * d) < (y * s)) && (p * d) < k) {

System.out.printf("Cheaper way is %f day tickets", p);

} else if (((k < (y * s))) && k < (p * d)) {

System.out.printf("cheaper way is a month card");

} else if (((p * d) < k) && (p * d) < (y * s)) {

System.out.printf("Cheaper way is %f day tickets", p);

} else if (((y * s) < k) && (y * s) < (p * d)) {

System.out.printf("Cheaper way is one time tickets");

} else if ((k < (p * d)) && k < (y * s)) {

System.out.printf("cheaper way is a month card");

} else if (((k == (p * d))) && k < (y * s)) {

System.out.printf("cheaper way is a month card");

} else if (((k == (y * s))) && k < (p * d)) {

System.out.printf("Cheaper way is a month card");

} else if (((y * s) == (p * d)) && (y * s) < k) {

System.out.printf("Cheaper way is one time tickets");

}

}

}

Answer

if ((k == (p * d) == (y * s)))

Isn't a valid expression. You can only do 1 comparison per boolean expression so this will evaluate to a boolean === the y * s double. You can join them instead. ie

if (k == (p * d) && k == (y * s))

Also

System.out.printf(""Cheaper option is single cards"")

Is wrong, you only use a single set of "'s for strings, and your missing the trailing ;

Comments