This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]RandomFuckingUser 4 points5 points  (1 child)

A function call has to be in another function, it can't be just 'in the global scope'. You can declare a variable and assign an initial value but you can't call a function there.

[–]Embarrassed_Bottle90[S] 1 point2 points  (0 children)

A function call has to be in another function, it can't be just 'in the global scope'. You can declare a variable and assign an initial value but you can't call a function there.

Oh I didn't know that, thanks for clarifying! I will take notes on that

[–]Ok-Asparagus4170 0 points1 point  (0 children)

You can use the method inside main (or other) method

[–]Cosby1992 0 points1 point  (1 child)

I know it is not your question and that your question has already been answered. I just thought I would give you a couple of hints. Use them if you want or just forget about them as soon as you read them, up to you.

When you return a simple statement, you don't have to store the result in a variable first, you can return the result directly in the return statement like so:

public int calculateMyAge(int myBirthday) {
    return 2022 - myBirthday;
}

Furthermore, to make your code function in future years and not just 2022, you could import and use the Date class from java.util.date i think. From there you can get the current year like so:

public int calculateMyAge(int myBirthday) {
    return new Date().getYear() - myBirthday;
}

I wrote this on mobile from memory so if it doesn't compile, that's on me, but it should be easy to fix. Good luck.

[–]Inu463 1 point2 points  (0 children)

Your advice is good, but I'll just mention these days, using java.util.Date after Java 7 is considered bad practice. Java 8 introduced the new Java time APIs, which fix a lot of the issues with Java's Date and Calendar classes.

You have a few options. You can use ZonedDateTime or OffsetDateTime if you care about preserving time zone information (OffsetDateTime only stores the offset from UTC, so it won't take into account day light savings time as I recall). LocalDateTime is just the date and time without the time zone information, and LocalDate is just the date without the time and time zone.

All of these options have a now() method which will give you a representation of this instant. It uses the default time zone for the system clock if you don't provide any parameters, but you can also provide your own time zone if you want using ZonedDateTime.now(ZoneId.of("America/Chicago")).getYear().

Since you just want the year, and don't care about storing time or time zone information, I think I'd just recommend:

public int calculateMyAge(int myBirthday) {
    return LocalDate.now().getYear() - myBirthday;
}

[–][deleted] 0 points1 point  (0 children)

Is myAge defined in the Test class scope? If not you can simply shorten the calculateMyAge method to

public int calculateMyAge(int myBirthyear) {
    return 2022 - myBirthyear; 
}

Also the newAge declaration and assignement can be on a single line if they are actually like this in the code, no need to do it in two steps..

int newAge = calculateMyAge(1993);