Click to See Complete Forum and Search --> : So confused......else if/ to loop solution


nvibest
10-08-2008, 12:19 AM
Hello all,

For the past 5 or so hours:confused::mad: I've been trying to figure out how to condense this basic code that uses nextInt(); to read the first user input integer.
import java.util.Scanner;
public class DateCalculator {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int month;
int day;
int year;
int[] dayCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter integers for month, day, and year using 4 digits for the year: ");
month = s.nextInt();
if (s.equals("1")){
month = dayCount[0];
}
else if (s.equals("2")){
month = dayCount[1];
}
else if (s.equals("3")){
month = dayCount[2];
}
else if (s.equals("4")){
month = dayCount[3];
}
else if (s.equals("5")){
month = dayCount[4];
}
else if (s.equals("6")){
month = dayCount[5];
}
else if (s.equals("7")){
month = dayCount[6];
}
else if (s.equals("8")){
month = dayCount[7];
}
else if (s.equals("9")){
month = dayCount[8];
}
else if (s.equals("10")){
month = dayCount[9];
}
else if (s.equals("11")){
month = dayCount[10];
}
else if (s.equals("12")){
month = dayCount[11];
}
day = s.nextInt();
year = s.nextInt();


The above code is what I would like to make short by using a 'for' / 'while' loop (nested) or by using the 'switch' statements.........

........This code below is just me testing for potential ways to accomplish this....

//Displays element value
if (){
day = dayCount[2];
for (int d=0; d<dayCount.length; d++){

}
}
else{
for (int item : dayCount) {
System.out.println(item);
System.out.println(day);
}
}



Then this is how I will display error messages if the user inputs the wrong integers ( I used the 'else if' statement so that if there is more than one error the program will only display one error message.I want the number the user inputs (1-12)--1 being January, 2 being February, etc.-- to be assigned a variable so that later on the program will know which how to differentiate between elements .........
......

if (year < 1582){
System.out.println("Invalid Year");
}
else if (month < 0 || month >12 ){
System.out.println("Invalid Month");
}
}
}


Any help would be greatly appreciated.

Thanks,

N

chazzy
10-08-2008, 06:13 AM
well, this big mess


month = s.nextInt();
if (s.equals("1")){
month = dayCount[0];
}
else if (s.equals("2")){
month = dayCount[1];
}
else if (s.equals("3")){
month = dayCount[2];
}
else if (s.equals("4")){
month = dayCount[3];
}
else if (s.equals("5")){
month = dayCount[4];
}
else if (s.equals("6")){
month = dayCount[5];
}
else if (s.equals("7")){
month = dayCount[6];
}
else if (s.equals("8")){
month = dayCount[7];
}
else if (s.equals("9")){
month = dayCount[8];
}
else if (s.equals("10")){
month = dayCount[9];
}
else if (s.equals("11")){
month = dayCount[10];
}
else if (s.equals("12")){
month = dayCount[11];
}

should be


int j = s.nextInt();
if(j >= 1 && j <= 12)
month = dayCount[j-1];
else
month = j; //probably a bad idea, but it's what your logic states

nvibest
10-08-2008, 02:14 PM
well, this big mess


month = s.nextInt();
if (s.equals("1")){
month = dayCount[0];
}
else if (s.equals("2")){
month = dayCount[1];
}
else if (s.equals("3")){
month = dayCount[2];
}
else if (s.equals("4")){
month = dayCount[3];
}
else if (s.equals("5")){
month = dayCount[4];
}
else if (s.equals("6")){
month = dayCount[5];
}
else if (s.equals("7")){
month = dayCount[6];
}
else if (s.equals("8")){
month = dayCount[7];
}
else if (s.equals("9")){
month = dayCount[8];
}
else if (s.equals("10")){
month = dayCount[9];
}
else if (s.equals("11")){
month = dayCount[10];
}
else if (s.equals("12")){
month = dayCount[11];
}

should be


int j = s.nextInt();
if(j >= 1 && j <= 12)
month = dayCount[j-1];
else
month = j; //probably a bad idea, but it's what your logic states


ha, thanks.

What makes it bad exactly?

chazzy
10-08-2008, 07:25 PM
because it allows month to be 17 or 24. what's the name of month #17?

nvibest
10-08-2008, 09:36 PM
because it allows month to be 17 or 24. what's the name of month #17?

I thought I made sure that wouldn't happen by having any month less than 0 or above 12 to be 'Invalid' with this code.....

...... if (year < 1582){
System.out.println("Invalid Year");
}
else if (month < 0 || month >12 ){
System.out.println("Invalid Month");
}

..?

chazzy
10-08-2008, 10:41 PM
but you do it after collecting input (at least that's what it looks like from the code you posted)

nvibest
10-09-2008, 01:53 AM
but you do it after collecting input (at least that's what it looks like from the code you posted)

whole code.....
import java.util.Scanner;
public class DateCalculator {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int month;
int day;
int year;
int[] dayCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println("Enter integers for month, day, and year using 4 digits for the year: ");

int j = s.nextInt();
if(j >= 1 && j <= 12) {
month = dayCount[j-1];
}
else{
month = j;
}
day = s.nextInt();
year = s.nextInt();

/*


//Displays element value; just an idea
if (){
day = dayCount[2];
for (int d=0; d<dayCount.length; d++){

}
}
else{
for (int item : dayCount) {
System.out.println(item);
System.out.println(day);
}
}
*///////////////



if (year < 1582){
System.out.println("Invalid Year");
}
else if (month < 0 || month >12 ){
System.out.println("Invalid Month");
}
else if(day < 0 || day >31){
System.out.println("Invalid Day");

}
System.out.println("Month is: " + month);


if (year % 4 == 0) {

// divisible by 4 but not 100?
if (year % 100 != 0) {
System.out.println(year + " is a leap year.");
}
//divisible by 4 and 100 and 400?
else if (year % 400 == 0) {
System.out.println(year + " is a leap year.");
}
// It is divisible by 4 and 100 but not 400
else {
System.out.println(year + " is not a leap year.");
}
}
// It is not divisible by 4.
else {
System.out.println(year + " is not a leap year.");
}

}

}