Home > Mobile >  I don't know how to solve that issue
I don't know how to solve that issue

Time:01-23

I'm new at Java and I'm trying to solve this task:

Write a program in Java that asks the user for his birth year, encoded as two digits (like "62"), and asks for the current year, also encoded as two digits (like 99). The program should output the user's correct age in years. And the user needs to be asked if he had birthday this year or not.

My code:

import java.util.Scanner;

public class Agecalculator2 {

    public static void main(String[] args) {
        Scanner TB = new Scanner(System.in);

        System.out.println("Have you had birthday this year?, type '1' for yes and type '0' for no");
        int YorN = TB.nextInt();

        System.out.println("Birthyear: ");
        int birthyear = TB.nextInt();

        System.out.println("Current year: ");
        int currentyear = TB.nextInt();

        if (birthyear > currentyear && YorN == 1) {
            int currentyear1 = (currentyear   100);
            int yearcalc = (currentyear1 - birthyear);
            System.out.println("Your age is: "   yearcalc);
        } else if (birthyear > currentyear && YorN == 0) {
            int currentyear1 = (currentyear   100);
            int yearcalc1 = (currentyear1 - birthyear - 1);
            System.out.println("Your age is: "   yearcalc1);
        } else if (birthyear == currentyear) {
            System.out.println("Your age is: 100");
        } else {
            int yearcalc = (currentyear - birthyear);
            System.out.println("Your age is: "   yearcalc);
        }
    }
}

Output:

Have you had birthday this year?, type '1' for yes and type '0' for no
0
Birthyear: 
05
Current year: 
23
Your age is: 18

It says that my age is 18 even when I said that I haven't had birthday this year.

Have you had birthday this year?, type '1' for yes and type '0' for no
1
Birthyear: 
05
Current year: 
23
Your age is: 18

As I tried it with yes it workedm it only does not work with no.

Another instance where everything works as expected:

Have you had birthday this year?, type '1' for yes and type '0' for no
0
Birthyear: 
(19)75 // The two digits in the paranthesis are not displayed by the scanner
Current year: 
(20)23 // The two digits in the paranthesis are not displayed by the scanner
Your age is: 47
Have you had birthday this year?, type '1' for yes and type '0' for no
1
Birthyear: 
75
Current year: 
23
Your age is: 48

Edit:

@Frakcool

Here is a better agecalculator which I have written,but it doesn't calculate the age with two digits it does it with four.Thats the problem.

import java.util.Scanner;

public class Agecalculator {

    public static void main(String[] args) {

        Scanner TB = new Scanner(System.in);

        System.out.println("What's your name?");
        String name = TB.nextLine();

        System.out.println("Which year do we have?");
        int currentyear = TB.nextInt();

        System.out.println("In which year you've been born?");
        int born = TB.nextInt();

        System.out.println("Have you had birthday this year?, type '1' for yes and type '0' for no");
        int birthdayThisYear = TB.nextInt();

        if (birthdayThisYear == 1) {
            System.out.println("Your name is "   name   " and you are "   (currentyear - born)   " years old!");
        } else if (birthdayThisYear == 0) {
            System.out.println("Your name is "   name   " and you are "   (currentyear - born - 1)   " years old!");

        }

    }

}

CodePudding user response:

For the scenario that you mention of:

YorN = 0
birthyear = 05
currentyear = 23

Do a paper debugging

Is birthyear > currentyear? NO

Then it skips first and second ifs

Are they equal? NO

Then execute last else statement

You're missing some validations there

I'm not going to provide the full solution as it's a homework but that's the overall issue with this

Also I'd recommend you to add

TB.nextLine()

After each TB.nextInt()

And please follow naming conventions and make your variable names more descriptive

  • FirstWordUpperCasedClass
  • firstWordLowerCasedVariable
  • firstWordLowerCasedMethod()
  • ALL_WORDS_UPPERCASED_CONSTANT

This will make your program easier to follow and understand for you and everyone else

For example:

TB, what does it mean? Should be scanner

YorN, I know it stands for "yes or no" but could be birthdayThisYear (easier to understand what is going on)

birthyear to birthYear the next word starts with and uppercased letter so you can read each word more easily


Fine, here's a sample code that does the calculation depending on if you've had a birthday this year or not, and according to the other calculations

Bear in mind that you can improve some other things in the calculation process, such as removing some conditionals in favor of adding the birthdayThisYear in the calculation as @sleepyhead suggested above

This also adds a method to do the calculations, rather than having it all in the main method, which is better as your code is reusable and faster to test, and easier to follow and understand

public class AgeCalculator {
    public static void main(String[] args) {
        int age1 = calculateAge(23, 93, 0); //Should return 30
        int age2 = calculateAge(23, 93, 1); //Should return 31
        int age3 = calculateAge(50, 20, 0); //Should return 30
        int age4 = calculateAge(50, 20, 1); //Should return 31

        System.out.println(age1);
        System.out.println(age2);
        System.out.println(age3);
        System.out.println(age4);

    }

    private static int calculateAge(int currentYear, int born, int birthdayThisYear) {
        if (birthdayThisYear == 0) {
            if (currentYear > born) {
                return currentYear - born;
            } else if (currentYear < born) {
                return 100 - (born - currentYear);
            }
        } else {
            if (currentYear > born) {
                return (currentYear - born)   1;
            } else if (currentYear < born) {
                return (100 - (born - currentYear))   1;
            }
        }
        return 0;
    }
}

If you run the above code you'll get this as answers:

30
31
30
31

CodePudding user response:

@Frakcool

Here is a better agecalculator which I have written,but it doesn't calculate the age with two digits it does it with four.Thats the problem.

import java.util.Scanner;

public class Agecalculator {

    public static void main(String[] args) {

        Scanner TB = new Scanner(System.in);

        System.out.println("What's your name?");
        String name = TB.nextLine();

        System.out.println("Which year do we have?");
        int currentyear = TB.nextInt();

        System.out.println("In which year you've been born?");
        int born = TB.nextInt();

        System.out.println("Have you had birthday this year?, type '1' for yes and type '0' for no");
        int birthdayThisYear = TB.nextInt();

        if (birthdayThisYear == 1) {
            System.out.println("Your name is "   name   " and you are "   (currentyear - born)   " years old!");
        } else if (birthdayThisYear == 0) {
            System.out.println("Your name is "   name   " and you are "   (currentyear - born - 1)   " years old!");

        }

    }

}

CodePudding user response:

Your code does not provide an else-block for the condition birthyear < currentyear.

  • Related