Java Tip Calculator for newbie










0















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,
but the method for splitting the bill always seems to think each customer pays 'infinity'.
I have my program set up in two classes: tipCalc1 and tipCalc2 (no points for originality of course).
The program appears to run without issue besides the 'infinity' issue.



Here's what I have so far. Any assistance appreciated, thanks.



***TipCalc1 Class:***

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;
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();
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");












share|improve this question






















  • Could you be more specific about your problem? What are expected outputs? What are you getting instead?

    – BitNinja
    May 18 '14 at 1:37






  • 4





    While you are still a beginner, now is the best time to get out of the habit of using double to store an amount of money. If you use double in this way, you will introduce errors as soon as you start doing arithmetic. I strongly recommend either using BigDecimal to store your money, or using int to store a number of cents.

    – Dawood ibn Kareem
    May 18 '14 at 1:41















0















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,
but the method for splitting the bill always seems to think each customer pays 'infinity'.
I have my program set up in two classes: tipCalc1 and tipCalc2 (no points for originality of course).
The program appears to run without issue besides the 'infinity' issue.



Here's what I have so far. Any assistance appreciated, thanks.



***TipCalc1 Class:***

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;
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();
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");












share|improve this question






















  • Could you be more specific about your problem? What are expected outputs? What are you getting instead?

    – BitNinja
    May 18 '14 at 1:37






  • 4





    While you are still a beginner, now is the best time to get out of the habit of using double to store an amount of money. If you use double in this way, you will introduce errors as soon as you start doing arithmetic. I strongly recommend either using BigDecimal to store your money, or using int to store a number of cents.

    – Dawood ibn Kareem
    May 18 '14 at 1:41













0












0








0








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,
but the method for splitting the bill always seems to think each customer pays 'infinity'.
I have my program set up in two classes: tipCalc1 and tipCalc2 (no points for originality of course).
The program appears to run without issue besides the 'infinity' issue.



Here's what I have so far. Any assistance appreciated, thanks.



***TipCalc1 Class:***

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;
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();
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");












share|improve this question














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,
but the method for splitting the bill always seems to think each customer pays 'infinity'.
I have my program set up in two classes: tipCalc1 and tipCalc2 (no points for originality of course).
The program appears to run without issue besides the 'infinity' issue.



Here's what I have so far. Any assistance appreciated, thanks.



***TipCalc1 Class:***

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;
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();
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");









java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 18 '14 at 1:35









user3648784user3648784

111




111












  • Could you be more specific about your problem? What are expected outputs? What are you getting instead?

    – BitNinja
    May 18 '14 at 1:37






  • 4





    While you are still a beginner, now is the best time to get out of the habit of using double to store an amount of money. If you use double in this way, you will introduce errors as soon as you start doing arithmetic. I strongly recommend either using BigDecimal to store your money, or using int to store a number of cents.

    – Dawood ibn Kareem
    May 18 '14 at 1:41

















  • Could you be more specific about your problem? What are expected outputs? What are you getting instead?

    – BitNinja
    May 18 '14 at 1:37






  • 4





    While you are still a beginner, now is the best time to get out of the habit of using double to store an amount of money. If you use double in this way, you will introduce errors as soon as you start doing arithmetic. I strongly recommend either using BigDecimal to store your money, or using int to store a number of cents.

    – Dawood ibn Kareem
    May 18 '14 at 1:41
















Could you be more specific about your problem? What are expected outputs? What are you getting instead?

– BitNinja
May 18 '14 at 1:37





Could you be more specific about your problem? What are expected outputs? What are you getting instead?

– BitNinja
May 18 '14 at 1:37




4




4





While you are still a beginner, now is the best time to get out of the habit of using double to store an amount of money. If you use double in this way, you will introduce errors as soon as you start doing arithmetic. I strongly recommend either using BigDecimal to store your money, or using int to store a number of cents.

– Dawood ibn Kareem
May 18 '14 at 1:41





While you are still a beginner, now is the best time to get out of the habit of using double to store an amount of money. If you use double in this way, you will introduce errors as soon as you start doing arithmetic. I strongly recommend either using BigDecimal to store your money, or using int to store a number of cents.

– Dawood ibn Kareem
May 18 '14 at 1:41












3 Answers
3






active

oldest

votes


















3














The default value for split (because you have not initialized it with another value) is 0.0, therefore, when you do



billPerPerson = total / split;


you divide by 0.0, so you will get Infinity.



Notes:



  • Since your variable splitPrompt is double and computers doesn't store real values with a 100% accuracy, you shouldn't compare it with 0.0. Since this variable will store 0 or 1 for input, you can declare it as int, which will be accurate.

  • Try to follow Java naming conventions. Use mixedCase for methods/variables and use CamelCase for classes/interfaces.


  • In the method split(), you should use an if-else if-else structure:



    if(splitPrompt == 0) 
    ...

    else if(splitPrompt == 1)
    ...

    else
    ...







share|improve this answer






























    1














    Silly mistake.



    Change



    System.out.println("How many ways would you like to split the bill?
    splitPrompt = scan.nextDouble();


    to



    System.out.println("How many ways would you like to split the bill?
    split = scan.nextDouble();


    since you never change split which, like all double variables, is initialized to 0.0.



    Also, you should use ints where appropriate as not all of the numbers should be doubles. Or even better, use 'y' and 'n' chars.






    share|improve this answer























    • Never mind, saw the second one...

      – awksp
      May 18 '14 at 1:40











    • @user3580294: I needed to make it more specific...

      – Hovercraft Full Of Eels
      May 18 '14 at 1:40











    • Yep, my apologies for jumping to conclusions a bit early

      – awksp
      May 18 '14 at 1:41


















    0














    Class TipCalc2



    //Total = **bill** * (gets percentage in decimal 15 = 0.15) + **bill**


    Line 18 needs to be:



    total = bill * (tip / 100) + bill;


    Line 36/37 needs to be:



    split = splitPrompt = scan.nextInt();
    billPerPerson = total / split;

    //You're dividing billPerPerson = total by ZERO (split);


    Line 36/37 original:



    billPerPerson = total / split;





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23717749%2fjava-tip-calculator-for-newbie%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      The default value for split (because you have not initialized it with another value) is 0.0, therefore, when you do



      billPerPerson = total / split;


      you divide by 0.0, so you will get Infinity.



      Notes:



      • Since your variable splitPrompt is double and computers doesn't store real values with a 100% accuracy, you shouldn't compare it with 0.0. Since this variable will store 0 or 1 for input, you can declare it as int, which will be accurate.

      • Try to follow Java naming conventions. Use mixedCase for methods/variables and use CamelCase for classes/interfaces.


      • In the method split(), you should use an if-else if-else structure:



        if(splitPrompt == 0) 
        ...

        else if(splitPrompt == 1)
        ...

        else
        ...







      share|improve this answer



























        3














        The default value for split (because you have not initialized it with another value) is 0.0, therefore, when you do



        billPerPerson = total / split;


        you divide by 0.0, so you will get Infinity.



        Notes:



        • Since your variable splitPrompt is double and computers doesn't store real values with a 100% accuracy, you shouldn't compare it with 0.0. Since this variable will store 0 or 1 for input, you can declare it as int, which will be accurate.

        • Try to follow Java naming conventions. Use mixedCase for methods/variables and use CamelCase for classes/interfaces.


        • In the method split(), you should use an if-else if-else structure:



          if(splitPrompt == 0) 
          ...

          else if(splitPrompt == 1)
          ...

          else
          ...







        share|improve this answer

























          3












          3








          3







          The default value for split (because you have not initialized it with another value) is 0.0, therefore, when you do



          billPerPerson = total / split;


          you divide by 0.0, so you will get Infinity.



          Notes:



          • Since your variable splitPrompt is double and computers doesn't store real values with a 100% accuracy, you shouldn't compare it with 0.0. Since this variable will store 0 or 1 for input, you can declare it as int, which will be accurate.

          • Try to follow Java naming conventions. Use mixedCase for methods/variables and use CamelCase for classes/interfaces.


          • In the method split(), you should use an if-else if-else structure:



            if(splitPrompt == 0) 
            ...

            else if(splitPrompt == 1)
            ...

            else
            ...







          share|improve this answer













          The default value for split (because you have not initialized it with another value) is 0.0, therefore, when you do



          billPerPerson = total / split;


          you divide by 0.0, so you will get Infinity.



          Notes:



          • Since your variable splitPrompt is double and computers doesn't store real values with a 100% accuracy, you shouldn't compare it with 0.0. Since this variable will store 0 or 1 for input, you can declare it as int, which will be accurate.

          • Try to follow Java naming conventions. Use mixedCase for methods/variables and use CamelCase for classes/interfaces.


          • In the method split(), you should use an if-else if-else structure:



            if(splitPrompt == 0) 
            ...

            else if(splitPrompt == 1)
            ...

            else
            ...








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 18 '14 at 1:38









          ChristianChristian

          27.5k43361




          27.5k43361























              1














              Silly mistake.



              Change



              System.out.println("How many ways would you like to split the bill?
              splitPrompt = scan.nextDouble();


              to



              System.out.println("How many ways would you like to split the bill?
              split = scan.nextDouble();


              since you never change split which, like all double variables, is initialized to 0.0.



              Also, you should use ints where appropriate as not all of the numbers should be doubles. Or even better, use 'y' and 'n' chars.






              share|improve this answer























              • Never mind, saw the second one...

                – awksp
                May 18 '14 at 1:40











              • @user3580294: I needed to make it more specific...

                – Hovercraft Full Of Eels
                May 18 '14 at 1:40











              • Yep, my apologies for jumping to conclusions a bit early

                – awksp
                May 18 '14 at 1:41















              1














              Silly mistake.



              Change



              System.out.println("How many ways would you like to split the bill?
              splitPrompt = scan.nextDouble();


              to



              System.out.println("How many ways would you like to split the bill?
              split = scan.nextDouble();


              since you never change split which, like all double variables, is initialized to 0.0.



              Also, you should use ints where appropriate as not all of the numbers should be doubles. Or even better, use 'y' and 'n' chars.






              share|improve this answer























              • Never mind, saw the second one...

                – awksp
                May 18 '14 at 1:40











              • @user3580294: I needed to make it more specific...

                – Hovercraft Full Of Eels
                May 18 '14 at 1:40











              • Yep, my apologies for jumping to conclusions a bit early

                – awksp
                May 18 '14 at 1:41













              1












              1








              1







              Silly mistake.



              Change



              System.out.println("How many ways would you like to split the bill?
              splitPrompt = scan.nextDouble();


              to



              System.out.println("How many ways would you like to split the bill?
              split = scan.nextDouble();


              since you never change split which, like all double variables, is initialized to 0.0.



              Also, you should use ints where appropriate as not all of the numbers should be doubles. Or even better, use 'y' and 'n' chars.






              share|improve this answer













              Silly mistake.



              Change



              System.out.println("How many ways would you like to split the bill?
              splitPrompt = scan.nextDouble();


              to



              System.out.println("How many ways would you like to split the bill?
              split = scan.nextDouble();


              since you never change split which, like all double variables, is initialized to 0.0.



              Also, you should use ints where appropriate as not all of the numbers should be doubles. Or even better, use 'y' and 'n' chars.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered May 18 '14 at 1:38









              Hovercraft Full Of EelsHovercraft Full Of Eels

              262k20213319




              262k20213319












              • Never mind, saw the second one...

                – awksp
                May 18 '14 at 1:40











              • @user3580294: I needed to make it more specific...

                – Hovercraft Full Of Eels
                May 18 '14 at 1:40











              • Yep, my apologies for jumping to conclusions a bit early

                – awksp
                May 18 '14 at 1:41

















              • Never mind, saw the second one...

                – awksp
                May 18 '14 at 1:40











              • @user3580294: I needed to make it more specific...

                – Hovercraft Full Of Eels
                May 18 '14 at 1:40











              • Yep, my apologies for jumping to conclusions a bit early

                – awksp
                May 18 '14 at 1:41
















              Never mind, saw the second one...

              – awksp
              May 18 '14 at 1:40





              Never mind, saw the second one...

              – awksp
              May 18 '14 at 1:40













              @user3580294: I needed to make it more specific...

              – Hovercraft Full Of Eels
              May 18 '14 at 1:40





              @user3580294: I needed to make it more specific...

              – Hovercraft Full Of Eels
              May 18 '14 at 1:40













              Yep, my apologies for jumping to conclusions a bit early

              – awksp
              May 18 '14 at 1:41





              Yep, my apologies for jumping to conclusions a bit early

              – awksp
              May 18 '14 at 1:41











              0














              Class TipCalc2



              //Total = **bill** * (gets percentage in decimal 15 = 0.15) + **bill**


              Line 18 needs to be:



              total = bill * (tip / 100) + bill;


              Line 36/37 needs to be:



              split = splitPrompt = scan.nextInt();
              billPerPerson = total / split;

              //You're dividing billPerPerson = total by ZERO (split);


              Line 36/37 original:



              billPerPerson = total / split;





              share|improve this answer





























                0














                Class TipCalc2



                //Total = **bill** * (gets percentage in decimal 15 = 0.15) + **bill**


                Line 18 needs to be:



                total = bill * (tip / 100) + bill;


                Line 36/37 needs to be:



                split = splitPrompt = scan.nextInt();
                billPerPerson = total / split;

                //You're dividing billPerPerson = total by ZERO (split);


                Line 36/37 original:



                billPerPerson = total / split;





                share|improve this answer



























                  0












                  0








                  0







                  Class TipCalc2



                  //Total = **bill** * (gets percentage in decimal 15 = 0.15) + **bill**


                  Line 18 needs to be:



                  total = bill * (tip / 100) + bill;


                  Line 36/37 needs to be:



                  split = splitPrompt = scan.nextInt();
                  billPerPerson = total / split;

                  //You're dividing billPerPerson = total by ZERO (split);


                  Line 36/37 original:



                  billPerPerson = total / split;





                  share|improve this answer















                  Class TipCalc2



                  //Total = **bill** * (gets percentage in decimal 15 = 0.15) + **bill**


                  Line 18 needs to be:



                  total = bill * (tip / 100) + bill;


                  Line 36/37 needs to be:



                  split = splitPrompt = scan.nextInt();
                  billPerPerson = total / split;

                  //You're dividing billPerPerson = total by ZERO (split);


                  Line 36/37 original:



                  billPerPerson = total / split;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 1:02









                  Stephen Rauch

                  30k153758




                  30k153758










                  answered Nov 16 '18 at 0:41









                  ReyRey

                  1




                  1



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23717749%2fjava-tip-calculator-for-newbie%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党