Hi there everyone, I am a novice at java and this one has me stumped. This is the beginning of a tax calculator but I am getting irregular output from the getIncome() method. It starts fine, letting the user enter income figures, but on the third income entry or so, the loop fails for one iteration then comes back. For the program to stop taking income figures, a negative number has to be entered, but when I do this I get blank output until I try again at which time the number is subtracted from the income total. I am not looking for a code fix, more where to look and the logic behind it as this is for an assignment and I don't want anyone else to do it for me. Anyway, here is the code (it is incomplete but the main method and the getIncome methods are what I am focused on at the moment). Thanks very much in advance for any and all help

import java.util.*;
public class Tax {

    public static void main(String[] args) {

    getIncome();
    //getExpenses();

    }

    public static void getIncome(){
        Scanner key = new Scanner(System.in);
        double income = 0;
        System.out.println("Welcome to Tax Calculator");
        System.out.println("Please enter your income:");
        income = key.nextDouble();
        while (income >0)
        {
            System.out.println("Please enter income:");
            income += key.nextDouble();
            System.out.println("Please enter income:");

        if (key.nextDouble()<0)  {   
            System.out.println("Thank you for using tax calculator, your total income entered is:" + income);

            System.exit(0);
        }
        else if (key.nextDouble() <0 || income <= 0){
                System.out.println("Thank you for using tax calculator,the program will now exit");
                System.exit(0);
        }

        }

        }
    public static void getExpenses(){
        Scanner in = new Scanner (System.in);
        double expenses = 0;
        while (expenses >0)
        {
        System.out.println("Please enter expenses");
        expenses += in.nextDouble();
        System.out.println("Please enter expenses");


        }
    }
}

Let's take a loop starting from line 16...

income = key.nextDouble();
while (income >0) {
  System.out.println("Please enter income:");
  income += key.nextDouble();
  System.out.println("Please enter income:");

  if (key.nextDouble()<0) {
    System.out.println("Thank you for using tax calculator, your total income entered is:" + income);
    System.exit(0);
  }
  else if (key.nextDouble() <0 || income <= 0) {
    System.out.println("Thank you for using tax calculator,the program will now exit");
    System.exit(0);
  }
}

What does line 16 do? The method nextDouble() is to accept an input from a user (you) via console and the input format is supposed to be a double -- number with/without decimal (i.e. 234, 2456.4, etc). Once it accepts an input from you, it returns the input as double data type. You have variable income to recieve the returned value from the method nextDouble().

Now look at line 23, you simply call key.nextDouble() without having a variable to hold the value. As a result, the method does its job -- prompt you and wait for an input -- and returns the values to if condition. At this point, the income variable is still the same. So there are 2 scenarios happen at line 23. One is if you enters a value less than 0, it will go into the if clause and executes correctly. The other is that you enter a value equal to or more than 0, it will go to else if clause. Then in the else if condition, you call for key.nextDouble() again which then prompts you to enter another value. That's what happening here.

The way to fix is to have 2 vairables. One is the sum of the income and the second is the value entered by a user. The sum will be initialize as 0. Each time a value is entered via key.nextDouble(), you either use it to check in the if-else condition or add it to your sum of income variable.

Don't try to optimize when you learn how to program. You could use as many variables as you want. The optimization will come later once you correctly implement a program.

The problem is here:

        System.out.println("Please enter income:");
        income += key.nextDouble();
        System.out.println("Please enter income:");
        if (key.nextDouble()<0)  {   
            System.out.println("Thank you for using tax calculator, your total income entered is:" + income);
            System.exit(0);
        }
        else if (key.nextDouble() <0 || income <= 0){
            System.out.println("Thank you for using tax calculator,the program will now exit");
            System.exit(0);
        }

You keep calling nextDouble in your if statements which ofcourse after a double has already been read it will read a white space.., rather read the double into a value and check it like that.

for example:

public static void getIncome() {
    Scanner key = new Scanner(System.in);
    double income = 0;
    System.out.println("Welcome to Tax Calculator");
    System.out.println("Please enter your income:");
    double d = key.nextDouble();
    income = d;
    while (income > 0) {
        System.out.println("Please enter income:");
        d = key.nextDouble();
        income += d;
        if (d < 0) {
            System.out.println("Thank you for using tax calculator, your total income entered is:" + income);
            System.exit(0);
        } else if (d < 0 || income <= 0) {
            System.out.println("Thank you for using tax calculator,the program will now exit");
            System.exit(0);
        }
    }
}

Thank you Taywin and David for your fast replies, both of your posts really helped, I understand now how taking the next.Double() would throw a wrench in the works. One more thing I was wondering, I need to have the program move on out of the getIncome method's while statement onto the getExpenses method when a negative number is entered without that negative number being subtracted from the income total. Would a try-catch statement to throw a negative number exception work for me in that regard? Thanks again for the quick replies!

What you could do instead of a try-catch is test the input before adding it to the total to see if it is negative, and if it is negative execute getExpenses(); For example, you could do something to this effect (within the body of getIncome)

double num;
System.out.print("Enter your income: ");
num=in.nextDouble();
if(num<0)
{
  getExpenses();
  return;
}
else
//rest of method here

Excellent, thanks for the help, I was knocking the idea of calling the next method but wasn't sure if it would include the negative number in the total before executing the called method. Thanks very much everyone for all the help!

What you could do instead of a try-catch is test the input before adding it

Actually your way of extracting getting expense out to a method in order to avoid try-catch does NOT really solve the problem. The try-cacth should still be used when you use nextDouble() or anything similar (nextInt() and nextFloat()). These type of method will take an input from users (could be as string), and then attempts to convert the input to the type which is to be returned -- nextDouble() returns double, etc. What do you think would happen if a user enter something with other character than a double? Of course, your program crashes because there is no error handling with NumberFormatException.

PS: There is no try-catch in the OP original post. What your explanation is just to wrap steps together in a method. Though, it is a good practice.

I agree Taywin, it doesn't address the problem of user error and I will have to account for that in more advanced assignments & units but for this one, the assignment says we are to assume the user will enter the correct information. Thanks again everyone for their input. I certainly apreciate the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.