dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

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

  1. #1
    Join Date
    Oct 2014
    Posts
    1

    Days Remaining Total Way Off, Displays Days Since Instead. Please Help?? Java SE 7

    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:
    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(birthMonthInt, 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 732635 days from now." It should read 138 days. What am I doing wrong?

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,855

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

    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)