Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Write code to declare an array that will hold calendar months (.e. January to December) java

public class App {

    public static String months[];

    public static void main ( String[] args ) {

        months = new String[ 13 ];
        months[ 0 ] = null;
        months[ 1 ] = "January";
        months[ 2 ] = "February";
        months[ 3 ] = "March";
        months[ 4 ] = "April";
        months[ 5 ] = "May";
        months[ 6 ] = "June";
        months[ 7 ] = "July";
        months[ 8 ] = "August";
        months[ 9 ] = "September";
        months[ 10 ] = "October";
        months[ 11 ] = "November";
        months[ 12 ] = "December";

        System.out.println ( "DEBUG args: " + Arrays.toString ( args ) );
        if ( args.length == 0 ) {
            System.out.println ( "No month specified. No arguments passed to 'main' method." );
        } else if ( args.length == 1 ) {  // Else we have a single argument as expected.
            int m = Integer.parseInt ( args[ 0 ] );
            System.out.println ( months[ m ] );
        } else if ( args.length > 1 ) {  // Else we have multiple arguments, but expected only one.
            System.out.println ( "ERROR - more than one argument passed to 'main' method." );
        } else {  // Else impossible. Should not reach this point. Defensive programming.
            System.out.println ( "ERROR - Unexpectedly reached IF-ELSE. Should be impossible." );
        }
…
Comment

Write code to declare an array that will hold calendar months (.e. January to December) java

package array2;

public class Array2 {

    static String months[];
    public static void main(String[] args) {
       months = new String[13];
       months[0] = null ;
       months[1] = "January";
       months[2] = "February";
       months[3] = "March";
       months[4] = "April";
       months[5] = "May";
       months[6] = "June";
       months[7] = "July";
       months[8] = "August";
       months[9] = "September";
       months[10] = "October";
       months[11] = "November";
       months[12] = "December";
       int m = Integer.parseInt( args[0] );
       System.out.println( months[ m ] );
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: input char arrayjava 
Java :: get sum of array and return string 
Java :: display 2d array 
Java :: miles to km converter programm in java 
Java :: t implements comparable 
Java :: javadoc commands 
Java :: final keyword in java 
Java :: how to insert a 0 in an array java 
Java :: java get number at the end of string 
Java :: HOW TO START ACTIVITY FROM FRAGMENT INSIDE? 
Java :: how to compare doubles in java 
Java :: while loops java 
Java :: java string replace 
Java :: java questions 
Java :: what is the max size of array in java 
Java :: convert string to int in java 
Java :: hashtable in java 
Java :: what does setFocusable do in java 
Java :: for() in java 
Java :: Java Method Create Basic 
Java :: path in spring form 
Java :: Java Enum compareTo() 
Java :: menu alert dialog in android 
Java :: Java Create a PrintWriter 
Java :: method overriding in java 
Java :: java put() method 
Java :: fabricmc concat text 
Java :: java truncate bigdecimal 
Java :: edit text on 2sec change andropid 
Java :: condensed for loop java 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =