Results 1 to 16 of 16

Thread: Quick help

  1. #1
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Quick help

    well im pretty new to java and ive been assigned this problem as part as hw


    "Given a string with a length >=2 and even
    If an 'a' occurs in the String then place a space on both sides of the 'a'. If the 'a' is not in the string then split the string in half"

    im thinking i have to use if and else statements but im not sure how to declare 'a' is the letter im looking for in java

  2. #2
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    To locate a character on a String use charAt() method!:)

  3. #3
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    i was considering that, but the script has to work with any word i put into the string

    word=("insert word here");

    if i use charAt() wont it only work for the number i put in charAt ?

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

    Default

    Quote Originally Posted by locazor View Post
    i was considering that, but the script has to work with any word i put into the string

    word=("insert word here");

    if i use charAt() wont it only work for the number i put in charAt ?
    Read the API documentation for the String class and pay special attention to the indexOf( ... ) method.

    kind regards,

    Jos

  5. #5
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    API documentation what do you mean by that? Thank you for your response as well. oh wow thanks a lot. I forgot that indexOf method identifies what you tell it to. I was thinking of using char the whole time.

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

    Default

    Quote Originally Posted by locazor View Post
    API documentation what do you mean by that?
    I mean this. Bookmark (or download) the thing because you can't do without it (noone can).

    kind regards,

    Jos

  7. #7
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    okay thanks again. Although in my Java class I'm restricted to an older version of Java.

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

    Default

    Quote Originally Posted by locazor View Post
    okay thanks again. Although in my Java class I'm restricted to an older version of Java.
    You can Google for versions of the API compatable for whatever version you are restricted to.

  9. #9
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    okay thanks again. This is what I have so far. which is the beginning. it gives me an error however when trying the If statement... is there a way to set the condition saying is word contains 'a' ?

    public class MakeUp2
    {
    public static void main(String []args)
    {
    String word, newWord, firsthalf, secondHalf;

    int len, letter;

    word=("adam");
    letter=word.indexOf("a");
    len=word.length();

    if(word==letter)

    System.out.println(letter);



    }
    }
    Last edited by locazor; 10-17-2010 at 05:09 PM.

  10. #10
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by locazor View Post
    String word
    int letter
    word is a String, letter is an Integer!

  11. #11
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    okay. so if statements break if i use two non compatible types. gotcha.

    i still dont see how i code into asking java if a string contains 'a' in an if statement
    Last edited by locazor; 10-17-2010 at 05:18 PM.

  12. #12
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by locazor View Post
    okay. so if statements break if i use two non compatible types. gotcha.

    i still dont see how i code into asking java if a string contains 'a' in an if statement
    ....you already have the code.

    Java Code:
    letter=word.indexOf("a");
    String (Java Platform SE 6))

    (or use indexOf('a'))

    Returns:
    if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

  13. #13
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    public class MakeUp2
    {
    public static void main(String []args)
    {
    String word, newWord, firstHalf, secondHalf;
    int len, letr;


    word=("bademe");

    letr=word.indexOf("a");
    len=word.length();

    if(len>=letr)

    {
    firstHalf=word.substring(0,letr);
    secondHalf=word.substring(letr+1);
    newWord=(firstHalf+" "+"a"+" "+secondHalf);
    }
    else
    {
    firstHalf=word.substring(0,len/2);
    secondHalf=word.substring(len/2);
    newWord=firstHalf+" "+secondHalf;
    }

    System.out.println(newWord);



    }
    }


    however the code breaks when i insert the word future in the string. it isnt going to the else portion. am i doing something wrong?

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

    Default

    Quote Originally Posted by locazor View Post
    if(len>=letr)
    This is always true (read the API documentation for the indexOf( ... ) method).

    kind regards,

    Jos

  15. #15
    locazor is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks a lot finally solved it. changed len>=letr to letr>=0

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

    Default

    Quote Originally Posted by locazor View Post
    thanks a lot finally solved it. changed len>=letr to letr>=0
    Yep, that's right; see? it's all in the API documentation; as I wrote: bookmark or download that gem.

    kind regards,

    Jos

Similar Threads

  1. Quick help please :confused:
    By Eimo in forum Advanced Java
    Replies: 0
    Last Post: 10-08-2010, 06:48 AM
  2. Quick help
    By Qwertypoiuy in forum New To Java
    Replies: 5
    Last Post: 01-06-2010, 10:49 PM
  3. Need help quick!
    By Manikyr in forum New To Java
    Replies: 11
    Last Post: 01-28-2009, 04:08 AM
  4. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 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
  •