Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to rewrite the main method to use as few lines of code as possible

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trying to rewrite the main method to use as few lines of code as possible

    Good afternoon everyone. I have an assignment in which I have to rewrite the following code into as few lines as possible and am wondering if anyone could offer any suggestions. The original code is:

    public static void main(String[] args)
    String s;
        boolean b;
        JOptionPane jop;
     
        jop = new JOptionPane();
     
        s = jop.showInputDialog("Enter your email address");
        b = s.matches(".*@.*\\..*");
        if (b)
        {
            System.out.println("Address Appears Valid");
        }
        else
        {
            System.out.println("Address is Invalid");
        }
    }

    So far I have condensed it to the following and am wondering if anyone could offer any suggestions to further reduce it into one line of code. Thank you in advance for any suggestions!

    public static void main( String[] args )
    {
       String s = JOptionPane.showInputDialog("Enter your email address");
     
       System.out.println(s.matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" );
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Trying to rewrite the main method to use as few lines of code as possible

    It's silly to try and write as few lines as possible because Java code doesn't really care about the concept of "lines": you can put everything into one line and barring line comments, have it work just as fine.

    public static void main( String[] args ){String s = JOptionPane.showInputDialog("Enter your email address");System.out.println(s.matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" );}

    Alternatively, don't store the result into s and just use it directly.

    Some of these simplifications will lead to poor code formatting, here are the simplifications I would make for writing "good" code:

    1. Don't need the boolean b.
    2. Use JOptionPane.showInputDialog()

    public static void main( String[] args )
    {
       String s = JOptionPane.showInputDialog("Enter your email address");
     
       if(s.matches(".*@.*\\..*"))
       {
          System.out.println("Address Appears Valid" );
       }
       else
       {
          System.out.println("Address is Invalid");
       }
    }

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trying to rewrite the main method to use as few lines of code as possible

    I'm supposed to rewrite all the code within the main method and reduce it to as few lines as possible, the goal being the code within the main method being reduced to one line. So I am wondering if there is a way to somehow combine the following two lines of code into one line of code and still achieve the same result:
    String s = JOptionPane.showInputDialog("Enter your email address");
     
    System.out.println(s.matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" );

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Trying to rewrite the main method to use as few lines of code as possible

    Yes, don't store the result into s and just use it directly. I wouldn't suggest doing this in practice, though as your line will become way too long and unreadable.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    jc100 (September 26th, 2013)

  6. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trying to rewrite the main method to use as few lines of code as possible

    Thanks!

Similar Threads

  1. Replies: 5
    Last Post: January 28th, 2013, 05:07 AM
  2. Can you rewrite this method without using LinkedList?
    By wholegrain in forum Java Theory & Questions
    Replies: 1
    Last Post: February 13th, 2012, 12:37 AM
  3. Help ! merge this code into 1 main method . ASAP :( tnx
    By anitsirc in forum Object Oriented Programming
    Replies: 2
    Last Post: November 23rd, 2011, 11:53 AM
  4. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  5. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM