0

This is my program and what I am trying to achieve is taking the char value in string amount1 at position 1 i.e for example amount1=$3.00 amount2=2. However it doesn't print out what I expected, 2.

System.out.print("$"+cost[0]+".00 remains to be paid. Enter coin or note: ");
String amount1 = keyboard.nextLine();
char amount2 = amount1.charAt(1);
String amountcheck = "$"+cost[0]+".00";
int [] remainder = new int[1];
    if (amount1.equals(amountcheck)) {
        System.out.println("Perfect! No change given."); }
    if (amount2 < cost[0]) {
        remainder[0] = cost[0] - amount2; }

System.out.println("Remainder = "+remainder[0]);
System.out.println(+amount2);

For example,

$3.00 remains to be paid. Enter coin or note: $2.00
Remainder = 0
50

The problem is both in lines 2 and 3. Firstly 3, It don't understand why it outputs 50 as char at amount1 index 1. If i'm not wrong don't char positions work similarly off array index systems. Secondly line 3, the if statement in lines of 8 and 9 of my original code don't seem to catch that amount 2 < cost[0] and don't do the following operations.

So what I expected to happen when I am taking char at position 1 of "$2.00" is newamount would be equal to 2 instead of 50 which the program is outputting.

I've tried changing the char positions but all this seems to do is decrement the value.

Alex Mack
  • 19
  • 2
  • 2
    `'2'` is different from `2`. One is a number, the other is a character representation. If you only want to get a single digit like you are doing right now. Just substract the character `'0'` (= `48`) to `char amount2` to get the digit value as an `int`. – AxelH Aug 25 '17 at 12:53
  • Apologies that is a mistake should be amount2. Look now. – Alex Mack Aug 25 '17 at 12:58
  • 1
    Still the same thing, you are using a `char` like it was a number. `'2'` is actually `50` in `int`, check an [ASCII table](http://www.asciitable.com/) to understand. I could post an answer but this would be more valuable for you if you play with `char` and understand. (even if I don't doubt someone else will do it ;) ) This is really simple – AxelH Aug 25 '17 at 13:01
  • See this [answer](https://stackoverflow.com/a/8148773/2226988) for better data types and libraries to handle currency. – Tom Blodget Aug 26 '17 at 13:52

2 Answers2

0

You set your amount2 as a char and when you print it, it translates to the ASCII number of that charater which for the charater '2' is 50. If you want to output 2 you should change your amount2 to type intand parse the char to integer like this Integer.parseInt(""+amount1.charAt(1)); in line 3.

Mirt Hlaj
  • 173
  • 10
  • Ok so adding the line newamount = Integer.parseInt(""+amount.charAt(1)); I get the error possible lossy conversion from int to char. I think I am utilising this command wrong. – Alex Mack Aug 25 '17 at 13:23
0

You are using a char to store a numeric value, but that value is a character, not a number. Meaning you are storing '2' which is actually 50 in ASCII. You should remove the value of 48 to get the correct value ('0') or parse the String into a number directly with Integer.parseInt(String).

But as I said in comment, this is easy to correct so I will not provide much more code to this.


But let's be honnest, your logic is risky from the beginning.

You are asking the user to input an amount in a specific format : "$#.00". If the number is two digit $##.00 it fails, if he add a space or don't put the $ or any mistake that users are professional to find, it fails.

First, you should simplified this, do you need the $ ? Ask for dollar currency if you want to specifiy it. Then, do you need decimals ? Let first assume you don't (see Note for decimal hint).

You just need to input an integer value through the Scanner, which provide method to get Integer -> Scanner.nextInt()

int ammountReceived = keyboard.nextInt();

Then you need to see if this is

  1. Equals
  2. Too much
  3. Not enough

Like this :

int remainder = amountToPay - amountReceived;
if(remainder == 0){
    //equals
} else if(remainder > 0){
    //not enough
} else {
    //too much
}

This would give a simpler solution of :

Scanner sc = new Scanner(System.in);
System.out.print("Amount to pay : $");
int amountToPay = sc.nextInt();

System.out.print("Amount received : $");
int amountReceived = sc.nextInt();
int remainder = amountToPay - amountReceived;
if (remainder == 0) {
    System.out.println("That perfect, thanks.");
} else if (remainder > 0) {
    System.out.println("Remaining : $" + remainder);
} else {
    System.out.println("Need to give back : $" + -remainder);
}
sc.close();

Yours and mine are close, but you will see this is more readable and focused on the problem, I don't play with char to get from a specific String pattern, I am focused on the problem -> to get paid ;)

Now, you have to add a loop here to ask again until you received the correct amount. But please, don't read a numerical String character by character...

Note :

  1. Scanner.nextInt throws exception if the format is incorrect (not an integer)
  2. You can easily adapt this to get double, float or better BigDecimal
AxelH
  • 14,325
  • 2
  • 25
  • 55