ternary statements
I am trying to extend this program so that if the user enters a customer reference number ending with D, they receive a discount beacuse they are paying by direct debit.
import java.util.Scanner;
publicclass electricitybill{
publicstaticvoid main(String args[]){
Scanner input =new Scanner(System.in);
finaldouble VAT = 0.05;
finaldouble OFFPEAK = 2.5;
finaldouble PEAK = 3.5;
int CustRefNum;
int PrevOffPeak, PresOffPeak;
int PrevPeak, PresPeak;
double PeakCost;
double OffPeakCost;
double TotalCost;
double TotalVAT;
System.out.println("Enter your Customer Reference Number");
CustRefNum = input.nextInt();
System.out.println("Enter your previous off peak rate");
PrevOffPeak = input.nextInt();
System.out.println("Enter your present off peak rate");
PresOffPeak = input.nextInt();
System.out.println("Enter your previous peak rate");
PrevPeak = input.nextInt();
System.out.println("Enter your present peak rate");
PresPeak = input.nextInt();
System.out.println("Customer Reference Number = " + CustRefNum);
System.out.println("Previous off peak rate = " + PrevOffPeak);
System.out.println("Present off peak rate = " + PresOffPeak);
System.out.println("Previous peak rate = " + PrevPeak);
System.out.println("Present peak rate = " + PresPeak);
OffPeakCost = (PresOffPeak - PrevOffPeak) * OFFPEAK;
PeakCost = (PresPeak - PrevPeak) * PEAK;
TotalCost = OffPeakCost + PeakCost;
TotalVAT = TotalCost * VAT;
GrandTotal = TotalCost + TotalVAT;
System.out.println("Off Peak Cost = " + OffPeakCost);
System.out.println("Peak Cost = " + PeakCost);
System.out.println("Total Cost = " + TotalCost);
System.out.println("Total VAT = " + TotalVAT);
System.out.println("Overall Total = " + GrandTotal);
This is what i have done so far but to be honest i am new to ternary statements.
finaldouble DDEBIT = 0.02;
double dDebit;
double DDTotal;
char letter;
letter ='D';
dDebit = CustRefNum.lastindexof('D') >-1 ? GrandTotal * DDEBIT : 0);
DDTotal = GrandTotal - dDebit;
System.out.println("Total cost of electricity bill with Discount is " + DDTotal);
Any help would be much appreciated.
[3724 byte] By [
java535a] at [2007-11-26 20:17:06]

What problem are you having?
Its coming up with an error on the ternary statement
> Its coming up with an error on the ternary statementGet rid of the last paren. And don't be afraid to actually read the error messages...~
> Its coming up with an error on the ternary statement*sigh*And the exact, complete text of the error message is...?
int cannot be dereferenced dDebit = CustRefNum.lastindexof('D') >-1 ? GrandTotal * DDEBIT : 0;
> int cannot be dereferencedCustRefNum is a primitive variable. Java doesn't allow you to call methods on primitives.~
> int cannot be dereferenced dDebit =
> CustRefNum.lastindexof('D') >-1 ? GrandTotal * DDEBIT
> : 0;
CustRefNum is an int. An int doesn't have methods, because it's a primitive. I'm assuming there's some String you want to use there, since you're looking for the last index of 'D'.
CustRefNum is an int. byte, char, short, int, long, float, double, boolean are primitves. They're simple values. They don't have fields or methods that you can access with the "dot".lastIndexOf is a method on the String class. It makes to sense to try to call it on an int.
> > Its coming up with an error on the ternary
> statement
>
> *sigh*
>
> And the exact, complete text of the error message
> is...?
Speaking of pulling teeth, this reminds me that I have a dentist appointment. Thanks.
Oh, and P.S. It's comforting to see continuing support for the theory about people who use the letters "java" in their screen names.
fair kop. so if i change custRefNum from an int to a string?
> fair kop. so if i change custRefNum from an int to a string?Feel free to do so but you want to check whether or not that string containsa 'D'. Do you suspect a 'D' present in a decimal string representationof an int?kind regards,Jos
well its supposed to work so that if a D is detected at the end of the customer reference number, the program runs with a discount because it signifies direct debit. And if there is no D, it runs normally
He is trying to tell you that a decimal integer cannot have a 'D' in it.
Every user input starts as a String.
So what you want to do is create an int from the string, up to but not including the trailing D (if it's present), right?
So you'll need two things: A string for the user input, which can end in a D, and an int for the int value of the string up to but not including the D. Note that the int neither ends with a D nor does not end with a D. The notion of ending with a D is not an issue. D is a character, and ints don't contain characters. There will also be no 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 in the int.
You'll also need to test whether the D was there at the end of the original string so that you know whether to apply the discount.
You're kind of trying to mush all that into one right now.
> Speaking of pulling teeth, this reminds me that I> have a dentist appointment. Thanks.Is he going to uproot all your teeth?
> Every user input starts as a String.
>
> So what you want to do is create an int from the
> string, up to but not including the trailing D (if
> it's present), right?
>
> So you'll need two things: A string for the user
> input, which can end in a D, and an int for the int
> value of the string up to but not including the D.
> Note that the int neither ends with a D nor does not
> end with a D. The notion of ending with a D is not an
> issue. D is a character, and ints don't contain
> characters. There will also be no 0, 1, 2, 3, 4, 5,
> 6, 7, 8, or 9 in the int.
>
> You'll also need to test whether the D was there at
> the end of the original string so that you know
> whether to apply the discount.
>
> You're kind of trying to mush all that into one right
> now.
I'm not too sure how to do this tho?
> I'm not too sure how to do this tho?
You want to check if a String ends with a 'D' right? Here goes:public boolean endsWith(String input, char aChar) {
return input != null && input.endsWith(""+aChar);
}
Note that I delegated most of the work to the Sring.endsWith(String)
method. Now suppose a String *does* end with a 'D', here's how you
can retrieve the prefix (without the 'D') part as an int:int num= 0;
try {
num= Integer.parseInt(input.substring(0, input.length()-1);
}
catch (NumberFormatException nfe) {
/* there was no valid number before the 'D' */
}
I'm sure you can put the pieces together now.
kind regards,
Jos
edit: fixed a typo;
JosAHa at 2007-7-21 17:54:16 >

> > Speaking of pulling teeth, this reminds me that I
> > have a dentist appointment. Thanks.
>
> Is he going to uproot all your teeth?
No silly, it's just that all this trying to get through to this guy is pretty much like how the expression goes -- "it's like pulling teeth".