Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Tip Calculator for newbie...

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tip Calculator for newbie...

    Hey all. I hope I'm posting in the right place.

    I'm pretty new to Java (meaning this is only my third program besides 'hello world').

    I have a tip calculator I'm working on for an assignment. I'm not getting an 'error' as such - the program runs, but the result is consistently wrong.

    I'm wondering if it's to do with the 'split' variable being a double, since the 'total' amount seems to be correct when run. The issue seems to be when it tries to divide the 'total' by 'split' - and consistently returns 'infinity' in the console.

    I have my program set up in two classes: tipCalc1 and tipCalc2 (no points for creative names of course). Here's what I have so far. Any assistance appreciated, thanks.

    TipCalc1

    import java.util.Scanner;
     
    public class Tipcalc1
    {  
        public static void main(String[] args)
        {
        	System.out.println("Welcome to Tip Calculator! ");
        	TipCalc2 Calculator = new TipCalc2();
            System.out.println("Please enter the bill amount: ");
            TipCalc2.calBill();
            System.out.println("What percentage would you like to tip?: ");
            Calculator.percTip();
     
        }
     
    }

    And the tipCalc2 class which does the dirty work:

    import java.util.Scanner;
     
    public class TipCalc2
    {
        static double bill;
        double tip;
        double total;
     
    //should 'split' be a double?//
     
        double split;
     
        double splitPrompt;
        double Y;
        double N;
        double billPerPerson;
     
            static Scanner scan = new Scanner(System.in);
            public static void calBill()
            {
                 bill = scan.nextDouble();
            }
     
            public void percTip()
            {
                 tip = scan.nextDouble();
                if(tip<1)
                {
                    total = bill * tip;
                }
            else total = bill * (tip/100);
            System.out.println("Your total is: " + total);
            Split();
            }
     
     
            public void Split()
            {
            System.out.println("Would you like to split the bill? ");
            System.out.println("Enter 1 for YES or 0 for NO: ");
     
            splitPrompt = scan.nextDouble();
            if(splitPrompt == 0)
            {
                System.out.println("Your total is: " + total);
                System.out.println("Thankyou. Goodbye.");
                System.out.println("End Program");  
            }
            if(splitPrompt == 1)
            {
                System.out.println("How many ways would you like to split the bill? ");
                splitPrompt = scan.nextDouble();
     
    //Problem may lie here, or further up where the split variable is defined (as double).//
     
                billPerPerson = total / split;
                System.out.println("Each person pays: " + billPerPerson);
                System.out.println("Thankyou. Goodbye.");
                System.out.println("End Program.");  
     
            }
            else System.out.println("Invalid Entry");
            }
     
        }
    Last edited by Nootoojaava; May 19th, 2014 at 01:54 AM. Reason: Added highlight tags.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Tip Calculator for newbie...

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Tip Calculator for newbie...

    Well I feel like an idiot now hehe.

    If anyone's interested, it was all about the 'split' - as I thought.
    Read the comments in the code to see what I mean.....


    public void Split()
    //This is not a variable - duhhhhhh (read below) hehe//
            {
            System.out.println("Would you like to split the bill? ");
            System.out.println("Enter 1 for YES or 0 for NO: ");
     
            splitPrompt = scan.nextDouble();
            if(splitPrompt == 0)
            {
                System.out.println("Your total is: " + total);
                System.out.println("Thankyou. Goodbye.");
                System.out.println("End Program");  
            }
            if(splitPrompt == 1)
            {
                System.out.println("How many ways would you like to split the bill? ");
                splitPrompt = scan.nextDouble();
     
    //Now here's where I face-planted the dirt.....//
     
                billPerPerson = total / split;
     
    //The problem was that I was calling a variable 'split' which didn't exist.//
     
                System.out.println("Each person pays: " + billPerPerson);
                System.out.println("Thankyou. Goodbye.");
                System.out.println("End Program.");  
     
            }
            else System.out.println("Invalid Entry");
            }

    All I needed was to declare a new variable instead of 'split' and use that instead.

Similar Threads

  1. Simple Printing Calculator w/ accumulator (Newbie programmer)
    By ASU_student87 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 13th, 2014, 03:54 AM
  2. Help Calculating a tip
    By Sephyncloud in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 16th, 2013, 07:58 AM
  3. Making a Better Tip Calculator
    By darthvader45 in forum Android Development
    Replies: 2
    Last Post: May 14th, 2013, 06:43 AM
  4. Could use some beginners tip here..
    By obie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 16th, 2012, 03:55 PM
  5. Tip of the Month
    By helloworld922 in forum The Cafe
    Replies: 6
    Last Post: July 5th, 2010, 02:42 AM