Page 1 of 3 123 LastLast
Results 1 to 20 of 48
  1. #1
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default Help needed with string manipulation

    This is what i have so far: and im at a loss of what to do next. spent hours and hours so far and am getting over it. please post helpful meaning code with explanations as i dont know much and am new to this world of programming.

    Thanks heaps.


    String input;
    String output;
    char current;


    System.out.println ("Enter a sentence:");
    input = scan.nextLine();
    current = input.charAt(0);
    if (current == 'a')
    output = output + current;
    System.out.println ("+output");




    }
    }
    Last edited by stringkilla; 08-30-2010 at 09:37 AM.

  2. #2
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    What exactly task you feel problematic?
    Is there any error?

  3. #3
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    firstly,

    output = output + current;

    is causing error. i want to add each individual char to output string one at a time.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    When you get errors, please post the full error message.

    In this case, the error is because output is not initialized; that is, you did not give it an initial value. Therefore, you cannot concatenate to it.

    Try replacing this:
    Java Code:
    String output;
    ...with this:
    Java Code:
    String output = "";
    This assignment looks quite fun actually, best of luck on it.

  5. #5
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    thanks very much, ill try and get back to you.

  6. #6
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    g'day again,

    program is doing what i want it to do thus far. now i just need to implement some more specific code to complete the task.

    this is what i have so far: (which compiles and runs correctly)



    String input;
    String output = "";
    char current;
    int charactar;

    System.out.println ("Enter a sentence:");
    input = scan.nextLine();
    current = input.charAt(0);
    if (current == 'a')
    output = output + current;
    System.out.println ("Content:"+output);


    }
    }

    after the if statement is performed, can i then loop it back around to do the if statement again for the charAt(1), then charAt(2) and so on? if so please show me.

    thanks
    Last edited by stringkilla; 08-30-2010 at 09:37 AM.

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

    Default

    Quote Originally Posted by stringkilla View Post
    after the if statement is performed, can i then loop it back around to do the if statement again for the charAt(1), then charAt(2) and so on? if so please show me.
    You already mentioned it yourself: a loop; it would surprise me if your textbook doesn't explain those in great detail. Do some research first.

    kind regards,

    Jos

  8. #8
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    ok so i know how to do while loops sort of but how do i implement:

    input.charAt(from 1'st to last character of string) ???

    instead of doing input.charAt(0) (1) (2)... and so on

    is it something like:
    int length;
    get the length of string (e.g. 25 characters)
    length = input.length()
    then going:

    while (input.charAt(o) < 25)

    or something

    help :(

    thanks

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    how do i implement:

    input.charAt(from 1'st to last character of string) ???
    Use a for loop with the index starting at 0(the first char) to the last char(index is length of string -1)
    Use the loop index variable(for example i) as the arg to the charAt(i) method.

    If you use a while loop, you will have to do all the index handling your self: Initialize, test and increment that is easily done by a for loop

  10. #10
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    sorry norm but that did not help me. rethink you reply to my previous post taking into account the assignment question and the relevance of your answer. i am asking for the answer. or some small portion of code that will help my program so far. have a look at what ive done. im stuck here. can you help me in the way i want you to and add to this.

    thanks.


    String input;
    String output = "";
    char current;
    int length;

    System.out.println ("Enter a sentence:");
    input = scan.nextLine();
    length = input.length();
    current = input.charAt(0);
    for (int length = 0; length <input.length(); length++) // dont know about this but i think im on to something
    if (current == 'a')


    output = output + current;
    System.out.println ("Content:"+output);





    be HELPFUL please i do not have your knowledge.
    Last edited by stringkilla; 08-30-2010 at 07:45 AM.

  11. #11
    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 stringkilla View Post
    sorry norm but that did not help me. rethink you reply to my previous post taking into account the assignment question and the relevance of your answer. i am asking for the answer. or some small portion of code that will help my program so far. have a look at what ive done. im stuck here. can you help me in the way i want you to and add to this.
    ....
    be HELPFUL please i do not have your knowledge.
    I beg to differ as Norm was being helpful, very helpful in fact, but the problem is that you didn't understand some or all of his answer, and fortunately this can be fixed. We don't like giving "the answer" here but rather helping you find the answer as you will learn so much more this way. In order for us to help you better, what you may wish to do is to tell us exactly what about his answer you don't understand, and perhaps we can either explain it more fully or else show you a decent tutorial that will help you understand us better.

    Best of luck at solving your problem! :)

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

    Default

    Java Code:
    or (int length = 0; length <input.length(); length++) // dont know about this but i think im on to something
    Yes, you are on to something here! Have you worked with for loops before? If not, please check out the tutorial on how they are used:
    Using for loops

    You will want to add an opening and closing brace for this loop and inside use the length int variable, the index of the loop, in your input.charAt(length) method call.

    Keep at it and best of luck!

  13. #13
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    ok thanks very much, ill keep going with it and get back to you.

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    definetely keep posting you are closer than you might think

  15. #15
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    ok so now when running the program i get weird output. please run, type ab or a or aa and see. stumped again. am trying to get it so it will output " a." or "a " or "A ". and so on. but am getting weirdness. any suggestions??? thanks
    Last edited by stringkilla; 08-30-2010 at 07:46 AM.

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

    Default

    Quote Originally Posted by stringkilla View Post
    if(current == 'a' && current+1 == 'b')
    That is silly of course: if current == 'a' of course current+1 equals 'b'; what did you have in mind?

    kind regards,

    Jos

  17. #17
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    what? this is what i get!?!



    Enter a sentence:
    aaa
    aaaaaa
    C:\>

    please re-read last post. thanks
    Last edited by stringkilla; 09-13-2010 at 04:35 AM.

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

    Default

    Quote Originally Posted by stringkilla View Post
    what? this is what i get!?!

    [ snip ]

    please re-read last post. thanks
    I don't need to; that if condition is silly. b.t.w. change your output to:

    Java Code:
    System.out.print("["+output+"]");
    ... and you'll see how many times output is printed ...

    kind regards,

    Jos
    Last edited by JosAH; 08-27-2010 at 01:19 PM.

  19. #19
    stringkilla is offline Member
    Join Date
    Aug 2010
    Posts
    32
    Rep Power
    0

    Default

    now i get this:


    Enter a sentence:
    ab
    [a]

    Enter a sentence:
    a
    [a]

    Enter a sentence:
    aa
    [a][aa]

    Enter a sentence:
    abab
    [a][aa]

    Enter a sentence:
    ab
    [a]

    i want it to output:"ab"
    Last edited by stringkilla; 09-13-2010 at 04:36 AM.

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

    Default

    Quote Originally Posted by stringkilla View Post
    now i get this:

    C:\KXT101>java AssigOne210
    Enter a sentence:
    ab
    [a]
    C:\KXT101>java AssigOne210
    Enter a sentence:
    a
    [a]
    C:\KXT101>java AssigOne210
    Enter a sentence:
    aa
    [a][aa]
    C:\KXT101>java AssigOne210
    Enter a sentence:
    abab
    [a][aa]
    C:\KXT101>java AssigOne210
    Enter a sentence:
    ab
    [a]

    i want it to output:"ab"
    Your computer can't guess what you want (and neither can we); now if the current character is an 'a' you append it to your output String and print it. If that's not what you want you should change your program code.

    kind regards,

    Jos

Page 1 of 3 123 LastLast

Similar Threads

  1. Image Manipulation
    By sh100 in forum New To Java
    Replies: 0
    Last Post: 11-03-2009, 11:18 AM
  2. Image manipulation
    By spike72 in forum Java 2D
    Replies: 1
    Last Post: 08-28-2009, 09:45 PM
  3. Array manipulation
    By Ms.Ranjan in forum New To Java
    Replies: 9
    Last Post: 07-18-2008, 10:10 PM
  4. String manipulation example (Title case)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:04 AM
  5. String Manipulation Task
    By hiranya in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 12:07 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
  •