Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Suggestions to make cleaner code

Thread view: 
#2pencil - 25 Jun 2007 16:10 GMT
/**
*
* Date usage.
*
**/

import java.io.*;
import java.util.*;
import java.awt.*;
import java.applet.Applet;

/**
*
* @author #2pencil (www.akroncdnr.com)
*
**/

public class date_ex {
    public static void main(String[] args) throws IOException {
    //public void init() {
        Calendar now = Calendar.getInstance();
        int day=now.get(Calendar.DAY_OF_WEEK);
        int month=now.get(Calendar.MONTH);
        int date=now.get(Calendar.DAY_OF_MONTH);
        String strday = "";
        String strmonth = "";

        day=now.get(Calendar.DAY_OF_WEEK);
        month=now.get(Calendar.MONTH);
        date=now.get(Calendar.DAY_OF_MONTH);

        if(day==1)strday="Sunday";
        if(day==2)strday="Monday";
        if(day==3)strday="Tuesday";
        if(day==4)strday="Wednesday";
        if(day==5)strday="Thursday";
        if(day==6)strday="Friday";
        if(day==7)strday="Saturday";

        if(month==0)strmonth="January";
        if(month==1)strmonth="February";
        if(month==2)strmonth="March";
        if(month==3)strmonth="April";
        if(month==4)strmonth="May";
        if(month==5)strmonth="June";
        if(month==6)strmonth="July";
        if(month==7)strmonth="August";
        if(month==8)strmonth="September";
        if(month==9)strmonth="October";
        if(month==10)strmonth="November";
        if(month==11)strmonth="December";

        System.out.print("Hello!\n");
        System.out.print("Welcome: "+strday+" the "+date+" of "+strmonth
+"!");
    }
}

-#2pencil-
www.akroncdnr.com
www.TriviaRules.com
printdude1968@gmail.com - 25 Jun 2007 16:19 GMT
> /**
>  *
[quoted text clipped - 56 lines]
>
> -#2pencil-www.akroncdnr.comwww.TriviaRules.com

That set of if statements could probably be re-written as a switch
statement:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html
~kurt - 25 Jun 2007 16:21 GMT
>         if(day==1)strday="Sunday";
>         if(day==2)strday="Monday";
[quoted text clipped - 3 lines]
>         if(day==6)strday="Friday";
>         if(day==7)strday="Saturday";

enums could probably clean this up a bit.

- Kurt
bugbear - 25 Jun 2007 17:03 GMT
Would something like...

formatter = new SimpleDateFormat("'Welcome:'+EEEEEEEE+' the '+dd+'!'");

System.out.print("Hello!\n")
System.out.print(formatter.format(new Date()););

Suit?

(uncompiled or tested)

   BugBear
Tim B - 26 Jun 2007 02:23 GMT
> Would something like...
>
[quoted text clipped - 8 lines]
>
>     BugBear

That's along the lines of what I was thinking. Here's a compiled  and tested
version:
formatter = new SimpleDateFormat("'Welcome:' EEEE 'the' dd 'of' MMMM'!'");
Alex Hunsley - 25 Jun 2007 17:51 GMT
> /**
>  *
[quoted text clipped - 34 lines]
>         if(day==6)strday="Friday";
>         if(day==7)strday="Saturday";

You could use an array here.

  String[] daysOfWeek = new String() {"Sunday", "Monday", "etc."};

  strday = daysOfWeek[day];

or you could use the date formatting classes, as someone else has suggested.

lex
Greg R. Broderick - 25 Jun 2007 20:24 GMT
#2pencil <number2pencil@gmail.com> wrote in news:1182784240.034919.238870
@w5g2000hsg.googlegroups.com:

> /**
>  *
[quoted text clipped - 3 lines]
>
> import java.io.*;

Code doesn't use any io classes, no need to import them.

> import java.util.*;

Sloppy coding to import entire package, especially in the case of java.util,
because there's a class name collision between java.util.Date and
java.sql.Date.  Better to explicitly name the classes you want to import.

> import java.awt.*;

Code doesn't use any AWT classes, no need to import them.

> import java.applet.Applet;

Code doesn't use Applet class, no need to import it.

> /**
>  *
[quoted text clipped - 3 lines]
>
> public class date_ex {

Class name does not follow Java naming conventions.

>      public static void main(String[] args) throws IOException {
>      //public void init() {
[quoted text clipped - 10 lines]
>
>           if(day==1)strday="Sunday";

Don't put the conditional on the same line as the condition -- when you're
single-stepping through the code in the debugger, it is very difficult to
tell whether you took the branch or not.  Instead, use:

          if(day==1)
              strday="Sunday";

>           if(day==2)strday="Monday";

Don't hard-code the day numbers.  java.util.Calendar has built-in constants
(public static final fields) for these.

>           if(day==3)strday="Tuesday";

Instead of doing any of this, why not just pass the date to
java.text.SimpleDateFormat, configured with the appropriate format string?

>           if(day==4)strday="Wednesday";

Your use of (or your failure to use) horizontal white space renders this code
considerably less legible.  I'd suggest

          if (day == 4)
              strday = "Wednesday";

>           if(day==5)strday="Thursday";
>           if(day==6)strday="Friday";
[quoted text clipped - 18 lines]
>     }
>}

Cheers!
GRB

Signature

---------------------------------------------------------------------
Greg R. Broderick                  usenet200705@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

#2pencil - 12 Jul 2007 18:35 GMT
On Jun 25, 3:24 pm, "Greg R. Broderick"
<usenet200...@blackholio.dyndns.org> wrote:
> #2pencil <number2pen...@gmail.com> wrote in news:1182784240.034919.238870
> @w5g2000hsg.googlegroups.com:
[quoted text clipped - 108 lines]
>
> - Show quoted text -

Thank you, this is exactly what I was looking for.  I've been writting
in C for many years, but am new to Java.  Sure it works, but it's best
to stop bad habbits before they begin!

Thanks again,
-#2pencil-
http://www.akroncdnr.com
http://www.greattrainnetwork.com
Roedy Green - 29 Jun 2007 04:02 GMT
>        if(day==1)strday="Sunday";
>        if(day==2)strday="Monday";
[quoted text clipped - 3 lines]
>        if(day==6)strday="Friday";
>        if(day==7)strday="Saturday";

try this:

private static final String daysOfTheWeek = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};

String dayOfWeek = daysOfTheWeek[ day ];

// not exactly the same as yours. In mine 0=Sunday
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 29 Jun 2007 08:02 GMT
#2pencil wrote:
>>         if(day==1)strday="Sunday";
>>         if(day==2)strday="Monday";
[quoted text clipped - 3 lines]
>>         if(day==6)strday="Friday";
>>         if(day==7)strday="Saturday";

<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#getDisplayName(int
,%20int,%20java.util.Locale
)>

as in

Calendar cal = obtainValue();
String day = cal.getDisplayName(
    Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.CANADA );
String month = cal.getDisplayName(
    Calendar.MONTH, Calendar.LONG, Locale.CANADA );
int dom = cal.get( Calendar.DAY_OF_MONTH );
System.out.println( "Welcome: "+ day +" the "+ dom +" of "+ month );

Ain't the Javadocs grand?

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.