Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple beginner Java problem involving strings. What am I doing wrong?

    The question is as follows..

    Suppose Joe is your best friend. Given a string, return "Hello Friend" if the string contains Joe (ignore the case so that JOE and joe also count as a friend). Return Hello followed by the string if it is not Joe. For example, if the string contains Bob then you would return "Hello Bob".

    helloFriend("Joe") ? "Hello Friend"
    helloFriend("Jo") ? "Hello Jo"
    helloFriend("JOE") ? "Hello Friend"

    Code:
    public String helloFriend(String name){
      if ("name".equals("Joe")){
        return "Hello " +"joe";}
      }
    Last edited by VIPStephan; 11-08-2016 at 11:09 AM. Reason: added closing code BB tag

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,785
    Thanks
    31
    Thanked 931 Times in 928 Posts
    Finding the word "joe" in a string is easy. Finding names without putting limits on them is impossible. It would be better to have the name entered into a <input> box and detect if it's joe or another. This works.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    div {
    	width: 255px;
    }
    button{
    	float: right;
    	margin-top: 10px;
    }
    </style>
    </head>
    <body>
    <div>
    Enter your name<input type="text" id="person_name">
    <button onclick="CheckName();">Continue</button>
    </div>
    <br><br>
    <div id="greetings"></div>
    <script>
    function CheckName(){
    	var str = document.getElementById("person_name").value;
    	if(str.toUpperCase() == "JOE")
    		document.getElementById("greetings").innerHTML = "Hello Friend";
    	else
    		document.getElementById("greetings").innerHTML = "Hello " + str;
    }
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Regular Coder
    Join Date
    Feb 2016
    Posts
    588
    Thanks
    0
    Thanked 129 Times in 127 Posts
    Seems to be Java code. Note that this Forum is for Javascript.

  4. #4
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,556
    Thanks
    6
    Thanked 1,245 Times in 1,215 Posts
    Moved to Java forum.

  5. #5
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are not working with seperate classes, you could do the following:

    <code>
    public static void main(String[] args) {
    System.out.println("Hello Friend " + setFriendName("Joe");
    }
    public static String setFriendName(String name) {
    String nameToLower = name.toLowerCase();
    if(nameToLower.equals("joe")){
    return "";
    } else {
    return name;
    }
    }
    </code>

  6. #6
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since I may not edit my previous post (yet):

    Code:
    public static void main(String[] args) {
            System.out.println("Hello Friend " + setFriendName("Joe");
    }
    public static String setFriendName(String name) {
            String nameToLower = name.toLowerCase();
            if(nameToLower.equals("joe")){
                return "";
            } else {
                return name;
            }
        }

  7. #7
    New to the CF scene
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ofcourse change 'Hellond ' on line 2 to 'Hello friend '.

  8. #8
    New to the CF scene
    Join Date
    Sep 2016
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by kasper6696 View Post
    The question is as follows..

    Suppose Joe is your best friend. Given a string, return "Hello Friend" if the string contains Joe (ignore the case so that JOE and joe also count as a friend). Return Hello followed by the string if it is not Joe. For example, if the string contains Bob then you would return "Hello Bob".

    helloFriend("Joe") ? "Hello Friend"
    helloFriend("Jo") ? "Hello Jo"
    helloFriend("JOE") ? "Hello Friend"

    Code:
    public String helloFriend(String name){
      if ("name".equals("Joe")){
        return "Hello " +"joe";}
      }
    Do something like this:
    Code:
    public String helloFriend(String name) {
        if (name.equals("Joe")) {
            return "Hello " + "Joe";
        }
    }


 

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
  •