www.webdeveloper.com
Results 1 to 7 of 7

Thread: So confused......else if/ to loop solution

  1. #1
    Join Date
    Sep 2008
    Posts
    13

    Question So confused......else if/ to loop solution

    Hello all,

    For the past 5 or so hours I've been trying to figure out how to condense this basic code that uses nextInt(); to read the first user input integer.
    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: ");
    		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....
    Code:
    //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 .........
    ......
    Code:
    		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

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    well, this big mess

    Code:
    		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

    Code:
    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
    Last edited by chazzy; 10-08-2008 at 05:17 AM.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  3. #3
    Join Date
    Sep 2008
    Posts
    13

    Question

    Quote Originally Posted by chazzy View Post
    well, this big mess

    Code:
    		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

    Code:
    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?

  4. #4
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    because it allows month to be 17 or 24. what's the name of month #17?
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  5. #5
    Join Date
    Sep 2008
    Posts
    13

    Question

    Quote Originally Posted by chazzy View Post
    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.....

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

  6. #6
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    but you do it after collecting input (at least that's what it looks like from the code you posted)
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  7. #7
    Join Date
    Sep 2008
    Posts
    13

    Thumbs up

    Quote Originally Posted by chazzy View Post
    but you do it after collecting input (at least that's what it looks like from the code you posted)
    whole code.....
    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.");
    		}
    
    	}
    
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles