r/javahelp • • Sep 13 '20

Homework I'm trying to create java program to calculate compound interest, what is my errors ?

I think the problem was about double and int but don't know how to fix it.

https://i.stack.imgur.com/3rEpC.png

16 Upvotes

5 comments sorted by

2

u/ProfessorWily Sep 13 '20

Ah I see what happened, one second.

Your problem is that you are taking two ints, r and n, and dividing them to try and make a double. That's not going to work, you have to cast them into doubles first and THEN divide.

1

u/Fit-Journalist6562 Sep 13 '20

thank you, you help me alot.

13

u/Mochi-Mo Sep 13 '20

Int p = ... stuff that isn't an int.

All the error is saying is that by declaring p as an int, but assigning non int values, you're doing to lose data.

Just change int p to double p and you're good. If for some reason you want it as a whole number, you can deal with that after (which should be a pretty simple Google search to figure out, but let me know if you get stuck).

2

u/McBashed Sep 13 '20

Sometimes when you use nextInt you also need to use scanner.nextLine because when you hit enter (newline character \n) it counts as the next input. So next line accounts for that and then you accept an integer again.

I remember this from my class, someone please correct me if I'm wrong

1

u/AutoModerator Sep 13 '20

It seems that you possibly have a screenshot of code in your post I'm trying to create java program to calculate compound interest, what is my errors ? in /r/javahelp.

Screenshots of code instead of actual code text is against the Code posting rules of /r/javahelp as is also outlined in the sidebar - Code posting.

  • Never submit screenshots of code instead of code text!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).

  • Pastebin for programs that consist of a single class only

  • Gist for multi-class programs, or programs that require additional files

  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.

  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot. Talk-to-the-bot is the new talk-to-the-hand. If you instead want the classic talk-to-the-hand, just message the moderators. ;)

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