Results 1 to 11 of 11
  1. #1
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Lightbulb Little problem with if and else's :D

    Hi all! I'm very new to java and i just made a little program where you need to create account and the information will be saved, but the problem is that almost whole code is about "if" and "else". So my question is that are there anyone that could help me change this source code more simple and better? (not just if and else everywhere) :D Thanks in advance.

    Java Code:
    import java.lang.*;
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String username;
            String password1;
            String password2;
            String destination;
            String newLine = System.getProperty("line.separator");
    
            System.out.println("--------------------------");
            System.out.println("------Create Account------");
            System.out.println("--------------------------");
            System.out.println("");
            System.out.println("- Minimum amount of letters required in username and password is 4");
            System.out.println("- Username and password cannot contain space");
            System.out.println("- Username and password cannot be the same");
            System.out.println("");
    
            boolean markerOk = false;
            while (!markerOk) {
    
                System.out.print("Username: ");
                username = scan.nextLine();
                if (username.length() < 4) {
                    markerOk = false;
                    System.out.println("");
                    System.out.println("Error!");
                    System.out.println("Username is too short.");
                    System.out.println("");
    
                } else if (username.contains(" ")) {
                    markerOk = false;
                    System.out.println("");
                    System.out.println("Error!");
                    System.out.println("Username cannot contain space.");
                    System.out.println("");
    
                } else {
    
                    System.out.print("Password: ");
                    password1 = scan.nextLine();
                    if (password1.length() < 4) {
                        markerOk = false;
                        System.out.println("");
                        System.out.println("Error!");
                        System.out.println("Username is too short.");
                        System.out.println("");
    
                    } else if (password1.contains(" ")) {
                        markerOk = false;
                        System.out.println("");
                        System.out.println("Error!");
                        System.out.println("Username cannot contain space.");
                        System.out.println("");
    
                    } else {
    
                        if (username.equals(password1)) {
                            System.out.println("");
                            System.out.println("Error!");
                            System.out.println("Username and password can't be the same.");
                            System.out.println("");
    
                        } else {
                            markerOk = true;
                            System.out.print("Confirm password: ");
                            password2 = scan.nextLine();
    
                            if (password1.equals(password2)) {
                                markerOk = true;
                                final Formatter x;
                                try {
                                    x = new Formatter("Account.txt");
                                    x.format("Username: " + username + newLine + "Password: " + password2);
                                    x.close();
                                    System.out.println("");
                                    System.out.println("Account succesfully created! Account information can be found in the same folder as this program");
                                    System.exit(0);
    
                                } catch (Exception e) {
                                    markerOk = false;
                                    System.out.println("");
                                    System.out.println("Error!");
                                    System.out.println("Couldn't save account information.");
                                    System.out.println("");
                                }
                            } else {
                                markerOk = false;
                                System.out.println("");
                                System.out.println("Error!");
                                System.out.println("Passwords doesn't match.");
                                System.out.println("");
                            }
                        }
                    }
                    markerOk = false;
                }
            }
        }
    }
    Last edited by krechlich; 03-14-2011 at 12:55 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krechlich View Post
    Hi all! I'm very new to java and i just made a little program where you need to create account and the information will be saved, but the problem is that almost whole code is about "if" and "else". So my question is that are there anyone that could help me change this source code more simple and better? (not just if and else everywhere) :D Thanks in advance.
    You do need all those tests (and you want to report every single failure) but there is no need to cram all that logic in one single method that zig-zags left and right all the time. Why not implement a few simple methods that test one single thing and report when the test fails; they should return an appropriate true/false value. Your main test boils down to:

    Java Code:
    if (isLongEnough(userName) && isLongEnough(passwd) &&
       containsNoSpace(userName) && containsNoSpace(passw) &&
       typeAgain(passwrd)) {
       // set the new username and password
    }
    I think the name of those methods tell you what they do ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by JosAH View Post
    You do need all those tests (and you want to report every single failure) but there is no need to cram all that logic in one single method that zig-zags left and right all the time. Why not implement a few simple methods that test one single thing and report when the test fails; they should return an appropriate true/false value. Your main test boils down to:

    Java Code:
    if (isLongEnough(userName) && isLongEnough(passwd) &&
       containsNoSpace(userName) && containsNoSpace(passw) &&
       typeAgain(passwrd)) {
       // set the new username and password
    }
    I think the name of those methods tell you what they do ...

    kind regards,

    Jos
    Now he's going to ask where to find those methods, however. ;-)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Now he's going to ask where to find those methods, however. ;-)
    Then we're all going to fart in his general direction ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You do need all those tests (and you want to report every single failure) but there is no need to cram all that logic in one single method that zig-zags left and right all the time. Why not implement a few simple methods that test one single thing and report when the test fails; they should return an appropriate true/false value. Your main test boils down to:

    Java Code:
    if (isLongEnough(userName) && isLongEnough(passwd) &&
       containsNoSpace(userName) && containsNoSpace(passw) &&
       typeAgain(passwrd)) {
       // set the new username and password
    }
    I think the name of those methods tell you what they do ...

    kind regards,

    Jos
    Hmm... is there anyway you could change the code for me? I didn't quite understant what you meant. As i said i'm beginner, started doing java just 4 days ago :P

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krechlich View Post
    Hmm... is there anyway you could change the code for me? I didn't quite understant what you meant. As i said i'm beginner, started doing java just 4 days ago :P
    Why do you want me to change my code suggestion? If you don't understand the idea behind it better stick to your cascaded if-else if ... statements. I'm sure your teacher won't complain.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Why do you want me to change my code suggestion? If you don't understand the idea behind it better stick to your cascaded if-else if ... statements. I'm sure your teacher won't complain.

    kind regards,

    Jos
    No, i meant could you try to change MY source code as you recommended. I really dont know how to change my code as you explained :D (modify it even a bit so i catch the point)

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by krechlich View Post
    No, i meant could you try to change MY source code as you recommended. I really dont know how to change my code as you explained :D (modify it even a bit so i catch the point)
    Ah, ok, I misunderstood; sorry about that. Implement a few simple methods that test one aspect of your username and password, e.g.

    Java Code:
    private static boolean isLongEnough(String s) {
       if (s.length < 4) {
          System.out.println("not long enough");
          return false;
       }
       return true;
    }
    implement little methods that check whether or not a String contains spaces etc. etc. Finally in your main test method you simply call all those little methods (as I showed you in a previous reply). You're still testing everything but the tests are spread over those little methods which improves readability of the code (imho).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Ah, ok, I misunderstood; sorry about that. Implement a few simple methods that test one aspect of your username and password, e.g.

    Java Code:
    private static boolean isLongEnough(String s) {
       if (s.length < 4) {
          System.out.println("not long enough");
          return false;
       }
       return true;
    }
    implement little methods that check whether or not a String contains spaces etc. etc. Finally in your main test method you simply call all those little methods (as I showed you in a previous reply). You're still testing everything but the tests are spread over those little methods which improves readability of the code (imho).

    kind regards,

    Jos
    OH!!! Now i understand. Thanks so much mate, but i think i will stuck on some point. Well anyways everything is good now :D
    Last edited by krechlich; 03-14-2011 at 05:10 PM.

  10. #10
    krechlich is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Question

    hmmm... btw, how does this code reduce if and elses? I added those private static booleans there and all, but how can it reduce those ifs and elses? Can you show me? (I didn't yet add those booleans to every place, only at the username point)

    Java Code:
    import java.lang.*;
    import java.util.*;
    
    public class Main {
    
        private static boolean contains(String contains) {
            if (contains.contains(" ")) {
                System.out.println("");
                System.out.println("Error!");
                System.out.println("Contains Space");
                System.out.println("");
                return true;
            }
            return false;
        }
    
        private static boolean isNotLongEnough(String isNotLongEnough) {
            if (isNotLongEnough.length() < 4) {
                System.out.println("");
                System.out.println("Error!");
                System.out.println("Not long enough");
                System.out.println("");
                return true;
            }
            return false;
        }
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String username;
            String password1;
            String password2;
            String newLine = System.getProperty("line.separator");
    
            System.out.println("--------------------------");
            System.out.println("------Create Account------");
            System.out.println("--------------------------");
            System.out.println("");
            System.out.println("- Minimum amount of letters required in username and password is 4");
            System.out.println("- Username and password cannot contain space");
            System.out.println("- Username and password cannot be the same");
            System.out.println("");
    
            boolean markerOk = false;
            while (!markerOk) {
    
                System.out.print("Username: ");
                username = scan.nextLine();
                if (contains(username) || isNotLongEnough(username)) {
                    markerOk = false;
                } else {
                    System.out.print("Password: ");
                    password1 = scan.nextLine();
                    if (password1.length() < 4 || password1.contains(" ")) {
                        markerOk = false;
                        System.out.println("");
                        System.out.println("Error!");
                        System.out.println("Password is too short or contains space.");
                        System.out.println("");
    
                    } else {
    
                        if (username.equals(password1)) {
                            System.out.println("");
                            System.out.println("Error!");
                            System.out.println("Username and password can't be the same.");
                            System.out.println("");
    
                        } else {
                            markerOk = true;
                            System.out.print("Confirm password: ");
                            password2 = scan.nextLine();
    
                            if (password1.equals(password2)) {
                                markerOk = true;
                                final Formatter x;
                                try {
                                    x = new Formatter("Account.txt");
                                    x.format("Username: " + username + newLine + "Password: " + password2);
                                    x.close();
                                    System.out.println("");
                                    System.out.println("Account succesfully created! Account information can be found in the same folder as this program");
                                    System.exit(0);
    
                                } catch (Exception e) {
                                    markerOk = false;
                                    System.out.println("");
                                    System.out.println("Error!");
                                    System.out.println("Couldn't save account information.");
                                    System.out.println("");
                                }
                            } else {
                                markerOk = false;
                                System.out.println("");
                                System.out.println("Error!");
                                System.out.println("Passwords doesn't match.");
                                System.out.println("");
                            }
                        }
                    }
                    markerOk = false;
                }
            }
        }
    }
    Last edited by krechlich; 03-14-2011 at 05:25 PM.

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. [SOLVED] if's &amp; else's again
    By dbashby in forum New To Java
    Replies: 9
    Last Post: 04-07-2009, 10:18 PM

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
  •