调用一个循环值

问题描述:

我已经有一个.txt文件,它包含一些bdays的月份,日期和年份,我可以找到它们之间的差异(假设有五个需要排序)。我做了一个for循环,可以找到日期之间的差异,但我似乎无法弄清楚如何计算for循环的转数(计算dayCount的五次差)。调用一个循环值

这是保存日期的Bday.txt文件。

1 01 2011  John Pickard 
    5 16 1968  Dad Apostol 
    2 12 2003  Pax Johnson 
    6 12 2009  Tired Joe 
    12 1 2002  Joshua Ike 

如果我们假设今天是2017年8月26日,两个日期之间的总数(使用jodatime)是

//this is sorted, and the CORRECT 
[-237, -195, -102, -75, 97] 
//it goes as Pickard, Johnson, Apostol, Joe, Ike 
//what the wrong output is 
[1, 2, 3, 4, 5] 
//me attempting to put dayCount = 1 inside a for loop. 

这里是我的代码:

import chn.util.*; 
import java.util.Arrays; 

import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.LocalDate; 
import org.joda.time.Days; 
class starter{ 
    /* 
    Trying to implement changes from mark 2.5 into mark 3 
    */ 
    public static void main(String args[]){ 

    //to orginize what todays date is. 
    EasyReader todaysmon; 
    EasyReader todayday; 
    EasyReader todayyear; 

    //month-day-year 

    //current 
    int month; 
    int day; 
    int year; 

    //the bday time month thing 
    int bmonth; 
    int bday; 
    int byear; 

    //set x = 0; x<numoflines; x++ 

    System.out.println(" "); 
    System.out.println(" "); 
    System.out.print("What is the month right now? "); 
    todaysmon = new EasyReader(); 
    month = todaysmon.readInt(); 
    System.out.print("What is today day? "); 
    todayday = new EasyReader(); 
    day = todayday.readInt(); 
    System.out.print("What is the year today? "); 
    todayyear = new EasyReader(); 
    year = todayyear.readInt(); 

    EasyReader database; 
    database = new EasyReader("Bday.txt"); 

    //x = to how many lines are in the .TXT file. 
    for (int x=0; x<5; x++){ 

    String[] myStrings = {database.readWord(),database.readWord(),database.readWord(),database.readWord(),database.readWord()}; 

    //System.out.println(Arrays.toString(myStrings)); 

     //this should do it with every value 

     //this is a test if the thing is intilizing correctly. 

     // for(int g = 0; g < myStrings.length; g++){ 
      // if(g == 0){ 
       //arrays go like this [0,1,2] 
      // System.out.println("This is the month value: "+ myStrings[0]); 
      // } 
     // } 

     //this will determine how maby days are the closest. 

      for(int diffinday = 0; diffinday < myStrings.length; diffinday++){ 

      //making another copy 

      bmonth = Integer.parseInt(myStrings[0]); 
      bday = Integer.parseInt(myStrings[1]); 
      byear = Integer.parseInt(myStrings[2]); 
      LocalDate start = new LocalDate(year,month,day); 
      LocalDate end = new LocalDate(year,bmonth,bday); 
      int dayCount = Days.daysBetween(start, end).getDays(); 
      System.out.println(dayCount); 

       if(dayCount < 0){ 
       System.out.println("This value is null!");    
       } 
       else{ 
       /*for now, we can only identify pos ints. we need to make an array that will 
       find the closest date, W/O IT BEING NEGATIVE*/ 
       System.out.println("The closest birthday is "+myStrings[4]); 
       } 
     } 
    } 
} 

}

+1

仅供参考,Joda-Time项目现在处于维护模式。该团队建议迁移到Java 8及更高版本中构建的java.time类。 –

试试这个:

public class DatesDiff { 
    private static String[] bdayDates = {"1 01 2011","5 16 1968","2 12 2003","6 12 2009","12 1 2002"}; 
    static int [] myArray = new int [bdayDates.length]; 
    public static void main(String[] args) { 
     for(int diffinday = 0; diffinday < bdayDates.length; diffinday++){ 
      String bday = bdayDates[diffinday]; 
      int bmonth = Integer.parseInt(bday.split(" ")[0]); 
      int birthday = Integer.parseInt(bday.split(" ")[1]); 

      LocalDate start = new LocalDate(LocalDate.now().getYear(),bmonth,birthday); 
      LocalDate end = LocalDate.now(); 
      int dayCount = Days.daysBetween(end, start).getDays(); 
      System.out.println(dayCount); 
      myArray[diffinday] = dayCount; 
      Arrays.sort(myArray); 
      System.out.println(Arrays.toString(myArray)); 

      if(dayCount < 0){ 
      System.out.println("This value is null!");    
      } 
      else{ 
      System.out.println("The closest birthday is "+bday); 
      } 
     } 
    } 
} 
+0

这不起作用,因为我需要单独的.txt文件中的日期。好的回答虽然 –

+0

一旦循环完成,希望你可以复制日期在单独的.txt文件。 – Unknown

+0

我需要从单独的文件中获取日期,并且能够使用EasyReader计算它在temp.java文件中的差异。最终,我的想法是,当我获得500多个日期时,我可以通过使for循环等于500+来轻松修改代码。虽然我不能将500个日期复制到数组中。这不是解决问题的有效方法。但感谢您的答案。 –