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 8 of 8

Thread: Days Remaining Total Way Off, Displays Days Since Instead. Please Help??

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Days Remaining Total Way Off, Displays Days Since Instead. Please Help??

    I'm supposed to calculate and display the number of total days, and then the number of months, and days from now (when the program is run) to when the person’s next birthday is (no leap year). Here is my code:

    import java.util.Scanner;
    import java.text.*;
    import java.lang.System;
    import java.util.Date;
    import java.util.Calendar;
     
    public class CSCD210Lab3
    {
       public static void main (String [] args)
       {
          Scanner userInput = new Scanner(System.in);
     
          //Declare Variables
     
          String nameFirst;
          char firstLetter;
          String nameMiddle;
          String nameLast;
          char lastLetter;
          String genders;
          String genderName;
          String nameReplace;
          String birthMonth;
          String birthDate;
          String birthYear;
          long birthMillis;
          long systemMillis;
          long diffMillis;
          int daysFromMillis;
          int birthMonthInt;
          int birthDayInt;
          int birthYearInt;
     
          //Get User Input
          System.out.print("Please Enter Your Full Name(Including Middle Name): ") ;
          nameFirst = userInput.next();
          nameMiddle = userInput.next();
          nameLast = userInput.next();
          firstLetter = nameFirst.charAt(0);
          lastLetter = nameLast.charAt(nameLast.length()-1);
          userInput.nextLine() ;
     
          System.out.print("Please Enter Your Birthday in the Following Format: MM DD YYYY: ") ;
          birthMonth = userInput.next();
          birthDate = userInput.next();
          birthYear = userInput.next();
          birthMonthInt = Integer.parseInt(birthMonth);
          birthDayInt = Integer.parseInt(birthDate);
          birthYearInt = Integer.parseInt(birthYear);
          Calendar myCalendar = Calendar.getInstance();
          myCalendar.set(birthYearInt, birthDayInt, birthYearInt);
          birthMillis = myCalendar.getTimeInMillis();
          systemMillis = System.currentTimeMillis();
          diffMillis = (systemMillis - birthMillis);
          daysFromMillis = (int)(diffMillis / (86400000));
          userInput.nextLine() ;
     
          DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
          Date date = new Date();
     
          System.out.print("Please Enter Your Gender as M or F: ") ;
          genders = userInput.next();
          char gender = genders.charAt(0);
          genderName = nameFirst + " " + nameMiddle + " " + nameLast;
          nameReplace = genderName.replaceAll(genders, "?");
          userInput.nextLine();
     
     
          System.out.println();
          System.out.println("The First Letter of Your First Name is: " + firstLetter) ;
          System.out.println("The Last Letter of Your Name is: "+ lastLetter);
          System.out.println("Your Middle Name is: " + nameMiddle);
          System.out.println("Your Name With the Last Name First is: "+ nameLast + ", "+ nameFirst + " " +nameMiddle);
          System.out.println("Today's Date is: "+dateFormat.format(date));
          System.out.println("Your Name With The Letter of Your Gender Replacing the Same Letters In Your Name is: "+ nameReplace);
          System.out.println("Your birthday is "+daysFromMillis+ " days from now, or  "+"months and "+" days.");
       }
    }



    When I call daysFromMillis in the print statement, if I enter in a birthday of 02/17/1993, I will get an answer of "your birthday is 5435 days from now." What am I doing wrong?
    Last edited by kjc21793; October 2nd, 2014 at 05:19 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Days Remaining Total Way Off?

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    What should the answer be?

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Days Remaining Total Way Off?

    Um, what? That's why I posted, I have no idea.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: Days Remaining Total Way Off, Displays Days Since Instead. Please Jelp??

    If those dates are too hard for you to compute the values for, I suggest that you chose some other dates that you can easily compute the values. That will enable you to verify that the program is working properly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Days Remaining Total Way Off, Displays Days Since Instead. Please Jelp??

    After rereading the question by Greg, it appears I misunderstood the question. Instead of 5435, it should read 138 days.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Days Remaining Total Way Off, Displays Days Since Instead. Please Help??

    Um, what? That's why I posted, I have no idea.
    That's hilarious! Thanks for a good chuckle. Did you think we'd know? (rhetorical)

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Days Remaining Total Way Off, Displays Days Since Instead. Please Help??

    Happy to provide some laughs. I first took it to mean like that question teachers always ask when you ask for help.

    --- Update ---

    I spotted an additional error in the code. On the line that reads:
    myCalendar.set(birthYearInt, birthDayInt, birthYearInt);

    I realized the first instance of birthYearInt should be replaced by birthMonthInt. After making the edits, the output is even worse than before. Instead of 5435 days, I now get an output of 732635 days, when it should read 138 days.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: Days Remaining Total Way Off, Displays Days Since Instead. Please Help??

    Also posted at: Days Remaining Total Way Off, Displays Days Since Instead. Please Help?? Java SE 7
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: August 27th, 2014, 12:58 AM
  2. leave the remaining html code after jsp:forward tag
    By nischalinn in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: June 19th, 2012, 07:40 AM
  3. Remaining compile errors: no suitable method found for & cannot find symbol
    By ChuckLep in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2011, 02:33 PM
  4. 12 days of xmas??? not showing
    By chonch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 08:45 PM
  5. how do i get only the workig days for a certain month
    By anonimus83 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 11th, 2010, 10:13 AM

Tags for this Thread