Results 1 to 5 of 5
  1. #1
    Mobix is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Unhappy Help needed: Basic subtraction problems

    I'm trying to found out how to do basic subtraction in java. I'm semi-new so I don't know everything, and everywhere I search all I can find is how to make a calculator.
    All I want to do is print a subtraction between two integers. I have tried doing,

    System.out.print(balance-price);

    but I am getting errors. Some places have told me to use two dashes but that doesn't work either. Please help!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help needed: Basic subtraction problems

    Quote Originally Posted by Mobix View Post
    but I am getting errors.
    You've made us so curious; what errors? Oh, the suspense ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Mobix is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help needed: Basic subtraction problems

    Quote Originally Posted by JosAH View Post
    You've made us so curious; what errors? Oh, the suspense ...

    kind regards,

    Jos
    bad operand types for binary '-'
    first type: java.lang.String; second type: int


    Im using BlueJ here is the full code
    Java Code:
    public class TicketMachine{
        private int price;
        private int balance;
        public TicketMachine(int ticketPrice){
            price = ticketPrice;
            balance = 0;
        }
        public void insertMoney(int moneyAmount){
            balance = balance+moneyAmount;
        }
        public int getPrice(){
            return price;
        }
        public int getBalance(){
            return balance;
        }
        public void buyTicket(){
            if(balance < price){
                System.out.println("You do not have enough money to purchase a ticket. ");
                System.out.print("You need " + price-balance + " more dollars for a ticket.");
            }
            else if(balance >= price){
                System.out.println("Here is your ticket. Your change is ");
                System.out.print(balance-price);
                System.out.println("Enjoy your day.");
            }
        }
    }
    Last edited by JosAH; 04-12-2012 at 08:33 PM. Reason: added [code] ... [/code] tags

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help needed: Basic subtraction problems

    Quote Originally Posted by Mobix View Post
    bad operand types for binary '-'
    first type: java.lang.String; second type: int

    Java Code:
                System.out.print("You need " + price-balance + " more dollars for a ticket.");
    This is the line where your compiler complained, right? Add parentheses around (price-balance) and you're in business.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Mobix is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help needed: Basic subtraction problems

    Quote Originally Posted by JosAH View Post
    This is the line where your compiler complained, right? Add parentheses around (price-balance) and you're in business.

    kind regards,

    Jos
    Thank You

Similar Threads

  1. Basic Java help needed
    By CaptainToenail in forum New To Java
    Replies: 5
    Last Post: 03-03-2013, 01:18 AM
  2. Double subtraction
    By bmbishop in forum New To Java
    Replies: 21
    Last Post: 04-21-2011, 08:03 AM
  3. Problems with a basic project
    By Goff256 in forum New To Java
    Replies: 11
    Last Post: 04-08-2011, 06:24 AM
  4. Replies: 3
    Last Post: 09-22-2009, 05:16 AM
  5. basic help needed
    By CurtisJC in forum New To Java
    Replies: 2
    Last Post: 03-02-2009, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •