简体   繁体   中英

How to print an interval of a 2D Array

So i have my 2D array initialized with data from a file and i was wondering if it's possible to System.out.print just this interval.

public static void OptionUn (String[][] TableauLectureFichier) {

    Scanner rDates = new Scanner(System.in);

    System.out.println("ENTER YOUR INTERVAL HERE IN THE FOLLOWING ORDER DAY1 MONTH1 DAY2 MONTH2");

    String Intervale = rDates.nextLine();

    String[] TabChaine = Intervale.split(" ");
    int[] tIntervale = new int[4];

    for (int i = 0; i <TabChaine.length; i++) {            
        tIntervale[i] = Integer.parseInt(TabChaine[i]);
    }

    int j1 = tIntervale[0],
        j2 = tIntervale[2],
        m1 = tIntervale[1],
        m2 = tIntervale[3];
}

My array basicly contains Temperature, each row is a temperature of a certain date, its all ordered for january 1st to december 31st. And my question is how can I only System.out.print the temperatures between the dates the USER enters. For exemple between January 1st and February first (1 1 1 2).

So the array looks as follow. It's an irregular 2D array

String[][] TableauLectureFichier = new String[12][];
        TableauLectureFichier[1] = new String[31];
        TableauLectureFichier[2] = new String[LeapYear];
        TableauLectureFichier[3] = new String[31];
        TableauLectureFichier[4] = new String[30];
        TableauLectureFichier[5] = new String[31];
        TableauLectureFichier[6] = new String[30];
        TableauLectureFichier[7] = new String[31];
        TableauLectureFichier[8] = new String[31];
        TableauLectureFichier[9] = new String[30];
        TableauLectureFichier[10] = new String[31];
        TableauLectureFichier[11] = new String[30];
        TableauLectureFichier[12] = new String[31];

Where LeapYear is a method that calculates if february has 28 or 29 days

I think you want to print from the two dimensional array passed to the function, and use the input from the user to print a selected range from the passed array?

Your issue must be, that simply doing two nested loops, will not solve this for you. Instead, you want to increment a calendar from the "start month" and "start day" to the "end month" and "end day".

Update

Ok, since you do not want to use a Calendar for this, you can do this by just iterating through the 2d array with a bit of logic. I will assume the the length of the second dimension of the array will vary depending on what month we are in. Meaning, that the code that supplies the data, will only have one entry for each valid day.

So, if the data is recorded on a leap year, the array dimension for February will simply be of the length 29. ( TableauLectureFichier[1].length == 29 )

Then you can do it with the following:

public static void OptionUn (String[][] TableauLectureFichier) {

    Scanner rDates = new Scanner(System.in);

    System.out.println("ENTER YOUR INTERVAL HERE IN THE FOLLOWING ORDER DAY1 MONTH1 DAY2 MONTH2");

    String Intervale = rDates.nextLine();

    String[] TabChaine = Intervale.split(" ");
    int[] tIntervale = new int[4];

    for (int i = 0; i <TabChaine.length; i++) {            
        tIntervale[i] = Integer.parseInt(TabChaine[i]);
    }

    int j1 = tIntervale[0],
        j2 = tIntervale[2],
        m1 = tIntervale[1],
        m2 = tIntervale[3];

    // Loop through all the months specified, taking care to convert from base 1 
    // to base 0
    for (int month = m1 - 1; month < m2; month++) {
        // Assume that we will start at the first day of a month
        int day = 0;

        // However, if the current month is the first, start from the day specified as j1 
        // instead, and convert to base 0 so that we can use it to index into the array
        if (month == m1) {
            day = j1 - 1;
        }

        // Assume that we will run till the end of this month, or in this case to the end
        // of the array for the current month
        int endDay = TableauLectureFichier[month].length;

        // If the current month is the last month, only run to specified end day
        if (month == m2) {
            endDay = j2;
        }

        // Now run from the start day to the end day in the current month, paying 
        // attention to that endDay is still base 1
        for (; day < endDay; day++) {
            System.out.println(String.format("Month: %d, day: %d, value %s",
                month, 
                day, 
                TableauLectureFichier[month][day]));
        }

        // At this point we will step into the next month
    }
}

I have not added bounds checking to the month or 'day' variables. So if you are handing this in to anyone - please add bounds checking before you use the variables in the array.

public static void OptionUn (String[][] TableauLectureFichier) {

Scanner rDates = new Scanner(System.in);

System.out.println("ENTER YOUR INTERVAL HERE IN THE FOLLOWING ORDER DAY1 MONTH1 DAY2 MONTH2");

String Intervale = rDates.nextLine();

String[] TabChaine = Intervale.split(" ");
int[] tIntervale = new int[4];

for (int i = 0; i <TabChaine.length; i++) {            
    tIntervale[i] = Integer.parseInt(TabChaine[i]);
}

int j1 = tIntervale[0],
    j2 = tIntervale[2],
    m1 = tIntervale[1],
    m2 = tIntervale[3];


for(int i=m1+1; i < m2; i++) {
    for(int j=j1+1; j < j2; j++) {
         System.out.printf("Temperature on day %d and month %d: %s", j, i,  TableauLectureFichier[i][j]);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM