Results 1 to 7 of 7

Thread: Java code

  1. #1
    swain_am is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Java code

    Hi guys;
    I don't know if any one can tell me how to write condition code for:

    the name needs to have at least two names – first name and last name; if not, the program will show an error message and ask the user the name again

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    You can use the split() method from the String class. This will split the original string into separate strings (depending on the regex) and place the different substrings into a String array. Then all you have to do is count the number of elements in the array.

    String split method(Java Platform SE 6))

    An example can be found half way down this page:
    Methods of the Pattern Class (The Java™ Tutorials > Essential Classes > Regular Expressions)

    More examples:
    Split a string using String.split() - Real's Java How-to
    Java String Split Example | Java Examples - Java Program Sample Source Code

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    swain_am is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much CJSLMAN

    The program should consist of only one class (Invest.java) and only one method (the main method).

    the lecturer said " You can use String variables" . does anyone know how to use that ?

    thanks,

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

    Default

    Your question as stated is a bit too broad and general to be able to be answered, other than sending you to the tutorials.

    What exactly don't you understand about using String variables or using variables in general?

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    These are the basics of String variables

    String variables are declared the same was as any other variable using
    Java Code:
    String myFirstString, mySecondString, myThirdString;
    their visibility can be set by placing public/protected/private at the beginning of the line. They come in handy when you have large numbers of long strings or want to allow the user to enter a string. To set the value of a String variable, use
    Java Code:
    myString = "This is my string";
    or set the value when declaring the variable:
    Java Code:
    String myString = "This is my string";
    
    or
    
    String myString = new String("This is my string");
    There is no import required for String variables.
    Also, to compare a string to a String variable in an if/while loop, use
    Java Code:
    if("This is my string".equals(myString))
    however, when comparing two string variables, you can simply use
    Java Code:
    if(string1 == string2)
    Hope this isn't too confusing
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    swain_am is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you. the string varaible is clear. My problem how can I do the condition to know the user enter enter at least two names !
    I am sorry! I am a new in java

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

    Default

    Chris mentioned String.split(...). If you call split(" ") on your String, it will return an array of all the words in the String that have a space between them. Then you can check that the length of the array is 2.

Similar Threads

  1. I need help on a practice code for java
    By Sageinquisitor in forum New To Java
    Replies: 12
    Last Post: 07-13-2010, 05:00 PM
  2. please help in java code (simple)
    By rahul_swe23 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 04:32 PM
  3. Java Code
    By g.ganiraju in forum New To Java
    Replies: 2
    Last Post: 10-20-2008, 03:18 PM
  4. java code
    By sobhalr in forum New To Java
    Replies: 0
    Last Post: 12-28-2007, 11:25 AM
  5. Need a java code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-11-2007, 11:06 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
  •