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 3 of 3

Thread: Comments or Suggestions on Some Code

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Comments or Suggestions on Some Code

    Hi all! I'm new to Java and programming and am taking my first programming course in college. No, this isn't homework help as I've completed that portion of the assignment. I'd simply like to do some extra coding, etc., to try to learn more. In other words, I'd like to go above and beyond. Anyway, I was hoping some of you experienced programmers could give me some pointers and or suggestions. Below is part of the program that we had to write. The teacher provided the code. We just had to type it in and change some things around to reflect our own made up company name and products. I was interested in adding my own method to the code that simply masks the last two characters of the user's password. So, below is my main method and the maskPassword method that I created. Did I implement the maskPassword method correctly? Even if it is correct, is it really the "right" way to do something like this? Initially, I named the method public void String mask password, but was getting errors along the lines of cannot use void with static something or another. I did some research and came to the conclusion that I needed to create an instance of the my class, so I added the code to create an instance of CompuTech, then did the string manipulation. Any feedback here would be very much appreciated!

    public class CompuTech {
     
        public static void main(String[] args) {
     
        CompuTech instance = new CompuTech();
     
        .....program displays a few input boxes and messages, etc.
     
        // build the various output strings
        nameOutputMsg = "Welcome " + customerName + "!\n"; 
        returnOutputMsg = "Your login is: " + userid + "/" + [B]instance.maskPassword(password)[/B] + ".\n\n"; 
        greetingOutputMsg = "Thank you for visiting CompuTech!\n";
     
        // this combines all the output strings and assigns them to one variable
        outputMsg = nameOutputMsg + returnOutputMsg + greetingOutputMsg;
     
        // displays the complete output string 
        JOptionPane.showMessageDialog( null, outputMsg );
        System.exit(0);
     
        } // end main
     
        // method to mask the last two characters of the password with asterisks
        private String maskPassword(String password){
            // if password is at least two characters long, mask last two characters
            if (password.length() > 1) {
              password = password.substring(0, password.length() - 2) + "**";
            }
            // password is only 1 character long, so just mask the one character
            else if (password.length() == 1) {
              password = password.substring(0, password.length() - 1) + "*";
            }
            else {
              password = "non entered"; 
            }
            return password;
        } // end maskPassword
     
    } // end Class CompuTech
    Last edited by mike_p; March 8th, 2012 at 04:50 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Comments or Suggestions on Some Code

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Comments or Suggestions on Some Code

    Thanks for the tip, Norm! Done.

Similar Threads

  1. Comments
    By igkarthi in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2012, 06:52 AM
  2. Optimize/Improvements/Suggestions for my code
    By JimmyNeutron in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 29th, 2011, 02:57 PM
  3. Suggestions how to do this?
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: September 2nd, 2010, 12:18 PM