HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

On What Day Is That Date

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thatdaywhatdate

Problem

I got bored on my long weekend so I decided to have my coding exercise. And I successfully made a program that let you input the day and the month then it gives you the day of given date. But sadly it's only for 2014 calendar.

import java.io.*;

public class DaysToYears{
public static void main(String[] args) throws ArrayIndexOutOfBoundsException{
  BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  try{
    String []yearName = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    String []days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int []yearsCode = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    int firstCase = 0;
    int secondCase = 0;
    String inputMonth = new String();
    int inputDate = 0;
      System.out.print("Enter your month: ");
        inputMonth = dataIn.readLine();
      System.out.print("Enter your date:  ");
        inputDate = Integer.parseInt(dataIn.readLine());
          for(int nodeCount=0;nodeCount<yearName.length;nodeCount++){
              if(inputMonth.equalsIgnoreCase(yearName[nodeCount])){
                if(inputDate < 7){
                    firstCase = (yearsCode[nodeCount] + inputDate + 3) % 7;
                    System.out.println(yearName[nodeCount] + " " + inputDate + " is " + days[firstCase]);
                    }else{
                      secondCase = (yearsCode[nodeCount] + inputDate + 3) % 7;
                      System.out.println(yearName[nodeCount] + " " + inputDate + " is " + days[secondCase]);
                      }
                    }
                  }
    }catch(IOException e){
      System.out.println("Error getting input!");
    }
  }
}

Solution

You're mixing input, computation, and output in one method. This is bad, as you can't use it anywhere else. All you have is a tool requiring user interaction, nothing what can be used from another code. Luckily, all you need here is to reorder the lines a bit and then "extract method". Your IDE will do it for you.

String inputMonth = new String();


As already said, this is unnecessary. Moreover, new String() is always wrong as it creates a new String, but all you could even need is

String inputMonth = "";


String inputMonth = new String();
int inputDate = 0;
  System.out.print("Enter your month: ");
    inputMonth = dataIn.readLine();
  System.out.print("Enter your date:  ");
    inputDate = Integer.parseInt(dataIn.readLine());


Fix indentation and move the declaration to initialization like

System.out.print("Enter your month: ");
String inputMonth = dataIn.readLine();
System.out.print("Enter your date:  ");
int inputDate = Integer.parseInt(dataIn.readLine());


to save lines and make it clearer.

}catch(IOException e){
  System.out.println("Error getting input!");
}


Imagine a 100k lines program you got to fix, which prints "Error getting input!" from time to time. Sure, you can search for the string (and find some hundreds of occurrences), but what about

} catch (IOException e) {
     e.printStackTrace()
}


(note the spacing) which would tell you immediately exactly what happened and where (and it'd go to stderr rather than stdout, which is better). Actually, in main, I never do this, simply

public static void main(String[] args) throws IOException {
    ...
}


does the job perfectly. Only handle exceptions if you can do something meaningful, otherwise let them propagate (or wrap them if needed). That's the simplest and best solution, just let it be.

Imagine java.io.* would "handle" the exception the way you do... you'd get some terminal output, but your program would know nothing about the problem.

Code Snippets

String inputMonth = new String();
String inputMonth = "";
String inputMonth = new String();
int inputDate = 0;
  System.out.print("Enter your month: ");
    inputMonth = dataIn.readLine();
  System.out.print("Enter your date:  ");
    inputDate = Integer.parseInt(dataIn.readLine());
System.out.print("Enter your month: ");
String inputMonth = dataIn.readLine();
System.out.print("Enter your date:  ");
int inputDate = Integer.parseInt(dataIn.readLine());
}catch(IOException e){
  System.out.println("Error getting input!");
}

Context

StackExchange Code Review Q#72121, answer score: 2

Revisions (0)

No revisions yet.