martines
martines

Reputation: 47

How to use my input in mathematical tasks

I am completely new to Java programming. I would like to use my data input (which in this case is my money to withdraw from "ATM"machine) and then divide it.

I tried to put: System.out.println(witdraw / a); but it says "The operator / is undefined for the argument type(s) java.lang.String, int"

Here is my code

package atm;

import java.util.Scanner;

public class ATM {

    public static void main(String[] args) {

    int a, b, c, d;
    a = 20;
    b = 10;
    c = 5;
    d = 1;

    Scanner user_input = new Scanner(System.in);

    String withdraw;
    System.out.print("Put an amount of \u0024 to withdraw: ");
    withdraw = user_input.next ();

    System.out.println(withdraw / a);

    System.out.println("Amount is \u0024 " + withdraw);

    user_input.close();  
    }
}

My goal is to display the USD denominations that will total that amount with the fewest number of bills $1, $5, $10, $20 where cents can be left as cents but for now I just want to know how can I use my input in mathematical task, such as division.

Upvotes: 1

Views: 108

Answers (1)

Joseph Larson
Joseph Larson

Reputation: 9058

The error message is a good hint. You can't do math on a string. You have to convert it to a number.

You can do it as a double with Double.parseDouble(withdraw) or as an int with Integer.parseInt(withdraw).

So your println would be:

System.out.println("Withdraw / a == " + Integer.parseInt(withdraw) / a);

Upvotes: 1

Related Questions