Results 1 to 18 of 18
  1. #1
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    hello guys!
    i have a case study goin on..and i need help regarding a null or a space entered on my program...if the user entered nothing(null or space), i wanted the program to return an "Invalid key" message or must ask the user to enter a name.

    btw, we are only able to use JOptionPane.

    Thanks
    Last edited by jLaurence; 12-05-2010 at 09:46 PM.

  2. #2
    java_newby is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    you would want to first use the trim method in string as this gets rid of spaces, so if the user just enters a space, after using trim() it is the same as entering null.
    then use if statement, get the users input, if input = null,
    bring up a JOptionPane...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by java_newby View Post
    you would want to first use the trim method in string as this gets rid of spaces, so if the user just enters a space, after using trim() it is the same as entering null.
    then use if statement, get the users input, if input = null,
    bring up a JOptionPane...
    I would check that the String returned isn't null before calling any methods on it, including trim. Otherwise you'll get a NPE.

  4. #4
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    got it from the other thread..thanks! :D
    Last edited by jLaurence; 12-05-2010 at 12:12 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  6. #6
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What "other thread"?
    from here link here

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Ah yes, that thread. Glad you have it solved.
    yeah temporarily.. :(
    can please check the code if this one is correct?

    Java Code:
    regUser[cntUser] = JOptionPane.showInputDialog("Enter your name: ");
    	cntUser++;
    	regUser[cntUser] = userName;
    	mMenu();
    	
    	if (userName == null || userName.trim().isEmpty() || userName.trim().length() == 0) {
    	JOptionPane.showMessageDialog(null, "You must enter a name.");
    	}
    	mMenu();

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

    Default

    Quote Originally Posted by jLaurence View Post
    Java Code:
    if (userName == null || userName.trim().isEmpty() || userName.trim().length() == 0) {
    That second and third test are identical; you can remove one of them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    That second and third test are identical; you can remove one of them.

    kind regards,

    Jos
    thanks Jos..
    but i cant make it work..
    im trying a forloop instead and use equalstoignorecase...
    need to know more syntax and other alternative methods.. (java nab :( oh well)

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

    Default

    Quote Originally Posted by jLaurence View Post
    thanks Jos..
    but i cant make it work..
    im trying a forloop instead and use equalstoignorecase...
    need to know more syntax and other alternative methods.. (java nab :( oh well)
    I don't think I understand your remark, but if the user entered null or just spaces the following condition is true:

    Java Code:
    if (userName == null || userName.trim().length() == 0) ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    oh sorry...im thinking of the other for loop for comparing an input and warns the user that the name is already exist... :)

    thanks jos..

    edit---cant make it work so here it is..

    Java Code:
    code deleted
    the code on switch case one..i need an output for "name already exists" when a user inputs a name twice and outputs "invalid name" if a user entered a null or space..

    :) can you help me with this guys?
    just the case 1 first..i need to learn more so i need to get it step by step
    Last edited by jLaurence; 12-05-2010 at 09:09 PM.

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

    Default

    Quote Originally Posted by jLaurence View Post
    oh sorry...im thinking of the other for loop for comparing an input and warns the user that the name is already exist... :)

    the code on switch case one..i need an output for "name already exists" when a user inputs a name twice and outputs "invalid name" if a user entered a null or space..

    :) can you help me with this guys?
    just the case 1 first..i need to learn more so i need to get it step by step
    Don't cram everything in one (main) method; create a little method, say, getUserName() that returns a valid user name and keeps on asking for a retry as long as the user entered null or an empty name. That'll clean up all that code in your main method and improves modularity and readability.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Don't cram everything in one (main) method; create a little method, say, getUserName() that returns a valid user name and keeps on asking for a retry as long as the user entered null or an empty name. That'll clean up all that code in your main method and improves modularity and readability.

    kind regards,

    Jos
    Ok master..thanks for the advise...im currently confused in using arrays and creating classes, esp the set/getVar() methods that's why the program i made is kinda messy right now..i'll create a method later when i remake the program..but for now can u help me with the case 1?
    did i code it right?

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

    Default

    Quote Originally Posted by jLaurence View Post
    Ok master..thanks for the advise...im currently confused in using arrays and creating classes, esp the set/getVar() methods that's why the program i made is kinda messy right now..i'll create a method later when i remake the program..but for now can u help me with the case 1?
    did i code it right?
    Nope, there should be a while statement somewhere: while user name is incorrect ... but please write a separate method for all this, it highly reduces the complexity of this all; especially when I see that you also don't want duplicate user names in your array.

    kind regards,

    Jos

    ps. You could always try to run your own code (it doesn't cost anything with those darn PCs nowadays) and see for yourself whether or not your code is correct.
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    papabear550 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    first I am going to ask the program if the user input is equals to space or null
    then I think I am going to give the user an error or a prompt that he/she entered nothing but space or null.. then I am going to command the program to return

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

    Default

    Quote Originally Posted by papabear550 View Post
    first I am going to ask the program if the user input is equals to space or null
    then I think I am going to give the user an error or a prompt that he/she entered nothing but space or null.. then I am going to command the program to return
    So translating your statements to code (step by step) we have:

    Java Code:
    while (true) { // keep on trying
       String userName= getAName(); // get anything from the user
       if (isEmpty(userName))    // handle a null or empty name
          warnForEmptyName();
       else if (alreadyExists(userName)) // handle a duplicate name
          warnForDuplicateName();
       else
          return userName; // yep, a new unique user name
    }
    Note the isEmptyName(), warnForEmptyName(), alreadyExists( ... ) and warnForDuplicateName() methods. You know what those methods have to do. I'm sure you can complete the above code snippet to another complete method. A lot of small methods don't hurt and they keep the code simple.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks Jos..
    i decided to remake the program and made some changes (including creating some methods for readability)

    now i got a new problem
    how can i get a result for my program (needs top scores that includes name and score)?
    is the array of string and array of int can be merged?
    i want the output to be this way using JOptionPane
    example:

    User Score
    jLaurence 88
    javaNoob 60

    score will be sorted from highest to lowest
    Thanks in advance

Similar Threads

  1. Replies: 6
    Last Post: 10-18-2010, 11:40 AM
  2. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  3. Replies: 2
    Last Post: 03-19-2010, 06:30 PM
  4. Please I need the solution to this
    By debobbt in forum New To Java
    Replies: 4
    Last Post: 12-18-2009, 05:34 AM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •