/**************************************************
*
* This program needs some cleaning up
*
***************************************************/
// Zach Krizan
// Data Structures 351-110
// September 08, 2010
package program1;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
String[] month; // reference to array
month = new String[12]; // make array
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
// create 2nd array
int[] numDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nElms2 = 12; // number of items in 2nd array
int i = 0; //loop counter
System.out.println("Array month is = ");
while (i<12)
{
System.out.println(month[i]); //will display contents of array
i++;
}
int ii= 0; //2nd loop counter
System.out.println("\nArray numDays is = ");
while (ii<12)
{
System.out.println(numDays[ii]); //will display contents of array
ii++;
}
String searchKey; //key of item to search for
int searchKey2; // key of item to search for in 2nd array
Scanner scan = new Scanner (System.in);
int numberMonth; // will be equal to what the user inputs
System.out.println("\nEnter the Number representing the Month: ");
numberMonth = scan.nextInt();
// System.out.println("\nYou entered: ");
//System.out.println(numberMonth + " and " + wordMonth + "\n");
String ifstat= " "; //variable used in the if statement
if (numberMonth == 1) //if the variable entered is equal to 1,
//then print the line January and so on...
{
System.out.println("January");
ifstat = month[0]; //sets up the next if statement
}
else if (numberMonth == 2)
{
System.out.println("February");
ifstat = month[1];
}
else if (numberMonth == 3)
{
System.out.println("March");
ifstat = month[2];
}
else if (numberMonth == 4)
{
System.out.println("April");
ifstat = month[3];
}
else if (numberMonth == 5)
{
System.out.println("May");
ifstat = month[4];
}
else if (numberMonth == 6)
{
System.out.println("June");
ifstat = month[5];
}
else if (numberMonth== 7)
{
System.out.println("July");
ifstat = month[6];
}
else if (numberMonth== 8)
{
System.out.println("August");
ifstat = month[7];
}
else if (numberMonth == 9)
{
System.out.println("September");
ifstat = month[8];
}
else if (numberMonth == 10)
{
System.out.println("October");
ifstat = month[9];
}
else if (numberMonth == 11)
{
System.out.println("November");
ifstat = month[10];
}
else if (numberMonth == 12)
{
System.out.println("December");
ifstat = month[11];
}
else // presents error message if the if statement wasn't
//equal to anythign
{
System.out.println("\nError in the number you entered");
}
if (ifstat == month[0])
// if the variable ifstat is equal the variable month[0] then print
// line so on.
{
System.out.println("has " + numDays[0] + " days");
}
else if (ifstat == month[1])
{
System.out.println("has " + numDays[1] + " days");
}
else if (ifstat == month[2])
{
System.out.println("has " + numDays[2] + " days");
}
else if (ifstat == month[3])
{
System.out.println("has " + numDays[3] + " days");
}
else if (ifstat == month[4])
{
System.out.println("has " + numDays[4] + " days");
}
else if (ifstat == month[5])
{
System.out.println("has " + numDays[5] + " days");
}
else if (ifstat == month[6])
{
System.out.println("has " + numDays[6] + " days");
}
else if (ifstat == month[7])
{
System.out.println("has " + numDays[7] + " days");
}
else if (ifstat == month[8])
{
System.out.println("has " + numDays[8] + " days");
}
else if (ifstat == month[9])
{
System.out.println("has " + numDays[9] + " days");
}
else if (ifstat == month[10])
{
System.out.println("has " + numDays[10] + " days");
}
else if (ifstat == month[11])
{
System.out.println("has " + numDays[11] + " days");
}
else // presents error if if statement is not equal to anything
{
System.out.println("\nTry again");
}
Scanner scan2 = new Scanner (System.in); //2nd scanner
String wordMonth; // will be equal to what the user inputs the 2nd time
System.out.println
("\nEnter the word representing the Month, please spell the full"
+ " word. Make sure the first word is in Upper case. \n");
wordMonth = scan2.nextLine();
String ifstat2 = " "; //used in the 2nd if statement
if (wordMonth.equals(month[0]))
// if wordMonth is equal to month[0] then print line and so on...
{
System.out.println("\nJanuary");
ifstat2 = month[0]; // sets up the next if statement
}
else if (wordMonth.equals(month[1]))
{
System.out.println("\nFebruary");
ifstat2 = month[1];
}
else if (wordMonth.equals(month[2]))
{
System.out.println("\nMarch");
ifstat2 = month[2];
}
else if (wordMonth.equals(month[3]))
{
System.out.println("\nApril");
ifstat2 = month[3];
}
else if (wordMonth.equals(month[4]))
{
System.out.println("\nMay");
ifstat2 = month[4];
}
else if (wordMonth.equals(month[5]))
{
System.out.println("\nJune");
ifstat2 = month[5];
}
else if (wordMonth.equals(month[6]))
{
System.out.println("\nJuly");
ifstat2 = month[6];
}
else if (wordMonth.equals(month[7]))
{
System.out.println("\nAugust");
ifstat2 = month[7];
}
else if (wordMonth.equals(month[8]))
{
System.out.println("\nSeptember");
ifstat2 = month[8];
}
else if (wordMonth.equals(month[9]))
{
System.out.println("\nOctober");
ifstat2 = month[9];
}
else if (wordMonth.equals(month[10]))
{
System.out.println("\nNovember");
ifstat2 = month[10];
}
else if (wordMonth.equals(month[11]))
{
System.out.println("\nDecember");
ifstat2 = month[11];
}
else // presents error message if the if statement doesn't match
{
System.out.println("\nError in word you entered.");
}
if (ifstat2.equals(month[0]))
// if the variable ifstat2 is equal to month[0] print line
{
System.out.println("has " + numDays[0] + " days");
}
else if (ifstat2.equals(month[1]))
{
System.out.println("has " + numDays[1] + " days");
}
else if (ifstat2.equals(month[2]))
{
System.out.println("has " + numDays[2] + " days");
}
else if (ifstat2.equals(month[3]))
{
System.out.println("has " + numDays[3] + " days");
}
else if (ifstat2.equals(month[4]))
{
System.out.println("has " + numDays[4] + " days");
}
else if (ifstat2.equals(month[5]))
{
System.out.println("has " + numDays[5] + " days");
}
else if (ifstat2.equals(month[6]))
{
System.out.println("has " + numDays[6] + " days");
}
else if (ifstat2.equals(month[7]))
{
System.out.println("has " + numDays[7] + " days");
}
else if (ifstat2.equals(month[8]))
{
System.out.println("has " + numDays[8] + " days");
}
else if (ifstat2.equals(month[9]))
{
System.out.println("has " + numDays[9] + " days");
}
else if (ifstat2.equals(month[10]))
{
System.out.println("has " + numDays[10] + " days");
}
else if (ifstat2.equals(month[11]))
{
System.out.println("has " + numDays[11] + " days");
}
else // presents error message
{
System.out.println("\nTry again");
}
}
}
No comments:
Post a Comment