Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Remainder Assignment Operator Help

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Remainder Assignment Operator Help

    Hello everyone. I'm completely new to java programming and need some help with using the remainder assignment operator. I want to make a program that calculates minutes that are inputed from a user, into years and days. I think I have the code to calculate the years correctly, but I don't know how to take the remainder from the result of the years calculation and translate it to days. When I input 1000000000 minutes, I get back 1902 years and 77 days. The correct answer should be 1902 years and 214 days. If anyone can point me in the right direction I would surely appreciate it. Thanks in advance.

    import java.util.Scanner;

    public class minuteCalc {
    public static void main(String[] args) {

    long minutes;
    double hours;
    double days;
    double totalYears;
    double totalDays;

    Scanner input = new Scanner(System.in);

    System.out.print("Enter the number of minutes: ");
    minutes = input.nextLong();

    hours = minutes / 60;
    days = hours / 24;
    totalYears = (int)days / 365;
    totalDays = (int)totalYears % 365; // ** this is the part I need help with **


    System.out.println(minutes + " minutes is approximately " + totalYears + " years and " + totalDays + " days.");


    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Remainder Assignment Operator Help

    Obviously totalYears % 365 is not correct. Imagine I enter the exact number of minutes in 1 year. The output should by 1 year and 0 days. However 1 % 365 = 1. What you need to do is a subtraction. You already have the total number of days in the days variable so you need to reduce it by the number of days in totalYears.
    Improving the world one idiot at a time!

Similar Threads

  1. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 02:49 PM
  2. about Operator Precedence
    By degar in forum Java Theory & Questions
    Replies: 6
    Last Post: August 12th, 2011, 01:57 AM
  3. Chinese Remainder therom
    By Joy123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 16th, 2011, 07:11 AM
  4. Sobel Operator (Edge Detection)
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 18th, 2011, 05:21 PM
  5. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 08:50 AM