简体   繁体   中英

How can I print out an Array position

What i am trying to do

Absences: Create an array that keeps track of the number of employees who were absent each day; it should be large enough to store a full month of data. For example, on the first day of the month, 1 employee was absent; on the second day, 0 employees were absent; 3 absences on the third day; and eventually on the last day of the month there were 2 absences. This data does NOT need to be input by the user.

Prompt the user to input a number; display the number of absences on the day they entered

what I have:

package absences;

import java.util.Scanner;

/**
 *
 * @author Matthew
 */
public class Absences {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);

       System.out.println("Enter the date.");

       int a = keyboard.next().charAt(0);


      int[] mArray;

      mArray = new int[32];

      mArray[0] = 0;
      mArray[1] = 1;
      mArray[2] = 0;
      mArray[3] = 3;
      mArray[4] = 0;
      mArray[5] = 2;
      mArray[6] = 1;
      mArray[7] = 0;
      mArray[8] = 0;
      mArray[9] = 0;
      mArray[10] = 0;
      mArray[11] = 0;
      mArray[12] = 0;
      mArray[13] = 0;
      mArray[14] = 0;
      mArray[15] = 0;
      mArray[16] = 0;
      mArray[17] = 0;
      mArray[18] = 0;
      mArray[19] = 0;
      mArray[20] = 0;
      mArray[21] = 0;
      mArray[22] = 0;
      mArray[23] = 0;
      mArray[24] = 0;
      mArray[25] = 0;
      mArray[26] = 0;
      mArray[27] = 0;
      mArray[28] = 0;
      mArray[29] = 0;
      mArray[30] = 0;
      mArray[31] = 2;

      System.out.println("On the Date " + a + "has " + + " absences" );

First off you can declare your array elements with a array list like so :

mArray = {0,0,0,20,0,2,20};//fill with your values

When printing out the number of absences on the day the user inputs, first check if the number is out of bounds

if(input > mArray.length-1 || input < 0){
    System.out.println("That is a invalid input!");
}else{
    System.out.println("Their were " + mArray[input] + " absences on day : " + input);

You need to check the input before calling that input to prevent OutOfBoundsExceptions which will crash your program.

System.out.println("On the Date " + a + "has " + mArray[a] + " absences" );

This is the direct answer for your question. As it is suggested in the previous post, you should perform boundary test.

I personally would suggest the following modifications. You can simplify your creation of the array, you need to check for invalid integers entered by using a while loop (and retrying until success) or (instead of a while loop) adding a try-catch block, and you can access the value that you were looking for from the array by using

mArray[a].

The following code utilising these suggestions, and modifies it using a while loop to deal with the ArrayIndexOutOfBoundsException that could be thrown. This can be removed or replaced with a try-catch if needed.

package absences;

import java.util.Scanner;

/**
 *
 * @author Matthew
 */
public class Absences {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        int a = -1;

        System.out.println("Enter the date.");

        while ( a < 0 || a > 31 )
        {
            try 
            {
                a = keyboard.nextInt();
            } catch (InputMismatchException e) {
                sc.next(); 
            }
            if ( a < 0 || a > 31 )
                System.out.println("Invalid date, try again!");
        }

        int[] mArray = {0,1,0,3,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}

        System.out.println("On the Date " + a + "has " + mArray[a] + " absences" );
    }
}

Also, Matthew correctly points out that there are no months with 32 days. When dealing with dates and times, I suggest you look into either the Calendar or the Time classes.

Edit: I have amended my code, as I overlooked the fact that your method of getting the int input would not work, as correctly pointed out by Preyas Shah in their answer.

Just one more thing. The previous answers, array bound checking seems good and should solve the problem. However, just one more thing. The method you are using:

int a = keyboard.next().charAt(0);

As per my understanding, it will just get the first character from the string, say if the date input is '23', it will only take '2'. so all the index values you get from user will be in single digit only. I checked out Java API, not sure if this can be useful instead.

int a = keyboard.nextInt();

Please let me know also, I'm trying to get better understanding as well.

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