Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: Boolean expression to put a name in proper form

  1. #1
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Boolean expression to put a name in proper form

    I am assigned to make a boolean method that takes a name as a parameter and determines wether or not it is in common form by seeing if there is a comma (standard form is last, first. non standard is first lasy.) . After it determines this, it is supposed to put it into common form if it is not already, by calling on the method that sees wether or not it has a comma. here is my code
    package name;
    import java.util.Scanner;
    /**
    *
    * @author Zack
    */
    public class Name {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    System.out.println("What is the name you want to test? ");
    String name = reader.nextLine();
    hasComma(name);
    }
    public static boolean hasComma(String name){
    boolean oName;
    int x= name.indexOf(',');
    if (x == -1) {
    oName= false;
    }else{
    oName= (true);
    }
    return (oName);
    }
    public static void convertName() {
    }
    }

    any help would be appreciated, thanks in advance.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Boolean expression to put a name in proper form

    What is your specific question. I see you have posted your requirements and I see you have posted your code. At a quick glance the code seems to do what you want. So what is your issue?

    BTW your hasComma method can be reduced to a single line of code.

  3. #3
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: Boolean expression to put a name in proper form

    Well right now, after I type in the name, it does not return anything, it just says build stopped and I have no Idea what to do for the convertname method.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Boolean expression to put a name in proper form

    Quote Originally Posted by zhider View Post
    it does not return anything
    Incorrect! The code does return a boolean value, it simply does nothing with it and the program ends.

  5. #5
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: Boolean expression to put a name in proper form

    How do I make it print the result?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Boolean expression to put a name in proper form

    How do you make a program print anything? Use a print statement! You can either wrap the print statement around the call to the hasComma method. Or you can assign the return value to a variable and then print the variable. If you do not know/understand either of those options then I suggest reading your text book or searching the web as there will be numerous examples of both.

  7. #7
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: Boolean expression to put a name in proper form

    Thank you so much for the help! you saved me. I only have one more thing to do on this code. I have to make it loop in the main method, asking for a name, until a blank is entered, what change would I have to make in main to do this? Thanks again for all of your help.

    public class Name {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    System.out.println("What is the name you want to test? ");
    String name = reader.nextLine();
    convertName(name);
    }
    public static boolean hasComma(String name){
    boolean oName;
    int x= name.indexOf(',');
    if (x == -1) {
    oName= false;
    }else{
    oName= (true);
    }
    return (oName);
    }
    public static boolean convertName(String name) {
    boolean convert = false;
    hasComma(name);
    if (hasComma(name)== true) {
    convert= true;
    }else{
    String s = name;
    String[] arr = s.split(" ");
    System.out.println (arr[1]+ ","+ arr[0]);
    }
    return (convert);
    }
    }

  8. #8
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Boolean expression to put a name in proper form

    Quote Originally Posted by Junky View Post
    BTW your hasComma method can be reduced to a single line of code.
    I can see how it could be cut down, but to a single line of code?.. Would you mind illustrating this, via a post or pm? I would like to see how, for my own personal knowledge...

    5 lines is the shortest I see it, excluding the signature.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Boolean expression to put a name in proper form

    Java Code:
    return name.indexOf(',') > -1;
    For the OP, please use [code] tags [/code] when posting code.
    Many of us won't bother reading unformatted code.
    penguinCoder likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Boolean expression to put a name in proper form

    Oh the irony!

    Tolls supplied the code despite his signature. But yes, that is exactly how I would have coded it.

  11. #11
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: Boolean expression to put a name in proper form

    You can simply use.

    if(name.contains(",")) return true;
    else return false;

    [Moderator edit: link removed]
    Last edited by DarrylBurke; 10-22-2012 at 04:07 AM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Boolean expression to put a name in proper form

    Quote Originally Posted by Junky View Post
    Oh the irony!

    Tolls supplied the code despite his signature. But yes, that is exactly how I would have coded it.
    I know...but I figured I'd let myself off, just this once.
    :)
    Anyway, the OP is clearly putting in some effort and seems to know what they're doing.

    As for gowthamgutha's code, how is that an improvement on my single line?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 10-27-2011, 06:40 PM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. Replies: 2
    Last Post: 07-18-2011, 07:11 AM
  4. [SOLVED] Boolean Expression Evaluation Framework
    By priyanka.dandekar in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2010, 02:35 PM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 04:11 AM

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
  •