Results 1 to 18 of 18
  1. #1
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default Is this the correct Output?

    so heres the code fragment.

    String einsteinQuote =
    "Peace Cannot be kept by force; it can" +
    " only be archived by understanding.";
    System.out.print (
    einsteinQuote.indexOf ("can") + "" +
    einsteinQuote.indexOf ("can", 7) + "" +
    einsteinQuote.lastIndexOf ("can"));
    I need to find the output of this. I'm thinking its "only be" since it starts from "can", and ends after 7 spaces over where its on "b(e)".
    making sure if i am getting the right output or not.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The best way of finding the output will be to write some runnable code and run it. (Whatever output you get it is, by definition, correct.)

    If you have difficulty understanding that output the first thing to do is to consult the String API documentation and, if you can't see what's going on, ask.

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

    Default

    I created the script, and I ran it, and all i got was 343434 or something.
    Of course I ran it before I came here to ask.

  4. #4
    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 Teny View Post
    I created the script, and I ran it, and all i got was 343434 or something.
    Of course I ran it before I came here to ask.
    This is important information to let us know in your first post. Also "or something" is not that helpful to us. Please give us the exact output and the code that produced it.

  5. #5
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    Sorry, this is what I got "343434" after i reran the .class

    Did i script it right?

    public class test
    {
    public static void main (String[] args)
    {

    String einsteinQuote =
    "Peace Cannot be kept by force; it can" +
    " only be archived by understanding.";
    System.out.print (
    einsteinQuote.indexOf ("can") + "" +
    einsteinQuote.indexOf ("can", 7) + "" +
    einsteinQuote.lastIndexOf ("can"));

    }
    }
    The Code fragment I got is from a PPT from the "Introduction to programming with Java" by John Dean chapter 5.

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

    Default

    Change your "" to " " and then count your letters to see if you are right. Let us know what comes up.
    In other words, change this:
    Java Code:
        String einsteinQuote = "Peace Cannot be kept by force; it can"
            + " only be archived by understanding.";
        System.out.print(einsteinQuote.indexOf("can") + ""  //no space between quotes
            + einsteinQuote.indexOf("can", 7) + ""  //no space between quotes
            + einsteinQuote.lastIndexOf("can"));
    to this:
    Java Code:
        String einsteinQuote = "Peace Cannot be kept by force; it can"
            + " only be archived by understanding.";
        System.out.print(einsteinQuote.indexOf("can") + " "  //space between quotes
            + einsteinQuote.indexOf("can", 7) + " "  //space between quotes
            + einsteinQuote.lastIndexOf("can"));
    Good luck.

  7. #7
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    This is my script so far
    public class test
    {
    public static void main (String[] args)
    {
    String einsteinQuote =
    "Peace cannot be kept by force; it can" +
    " only be achieved by understanding.";
    System.out.print(
    einsteinQuote.indexOf("can") + " " +
    einsteinQuote.indexOf("can", 7) + " " +
    einsteinQuote.lastIndexOf("can"));
    }
    }
    and my output was, "6 34 34".
    so does that mean I count to the number 6, and it displays the letters from 6 - 34?

    heres what the slide looks like


    The String Class's lastIndexOf Method


    The lastIndexOf methods are identical to the indexOf methods except that they search the calling-object string from right to left. For the one-parameter lastIndexOf method, the search starts from the rightmost character. For the two-parameter lastIndexOf method, the search starts from the position specified by the second parameter.

    What does this code fragment print?
    String einsteinQuote =
    "Peace cannot be kept by force; it can" +
    " only be achieved by understanding."
    System.out.print(
    einsteinQuote.indexOf("can") + " " +
    einsteinQuote.indexOf("can", 7) + " " +
    einsteinQuote.lastIndexOf("can"));
    Last edited by Teny; 04-13-2009 at 03:16 AM.

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

    Default

    So, you've figured it all out, correct?

  9. #9
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    Not Really, still a bit confused.
    The 'C' in cannot is #6, and #34 is the 'C' in can.
    So that means it displays "cannot be kept by force; it "?
    or am I getting something wrong?

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

    Default

    no your code is correct and the output is correct.

    this guy:
    Java Code:
    einsteinQuote.indexOf("can")
    outputs the first "can" it finds which you've counted to be at position 6.

    This guy:
    Java Code:
    einsteinQuote.indexOf("can", 7)
    finds the first "can" at position 7 or beyond, which you've counted out to be position 34,

    and this guy:
    Java Code:
    einsteinQuote.lastIndexOf("can"));
    finds the location of the last "can" which is the same as the one above. It all works fine.

  11. #11
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    I thought the "einsteinQuote.indexOf("can", 7)"
    was to start from the 'c' of can, and count to 7, and start from there. Does this start from the first 'can' (cannot) or the last 'can'?

    So the lastIndexOf(" ") is more of a place of where it should stop?
    Last edited by Teny; 04-13-2009 at 04:10 AM.

  12. #12
    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 Teny View Post
    I thought the "einsteinQuote.indexOf("can", 7)"
    was to start from the 'c' of can, and count to 7, and start from there. Does this start from the first 'can' (cannot) or the last 'can'?
    It starts from position 7 of the String (the "a" in "cannot") and looks for the next "can" in the String which is at position 34 (count it and you'll see). Remember to start your count with 0, not 1.

    So the lastIndexOf(" ") is more of a place of where it should stop?
    I don't quite get you here.

  13. #13
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    Yeah I did count it.
    Just to double check, what is the actual output?

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

    Default

    your numbers from your output are correct.

    Initially your String capitalized "Cannot" which meant that the first "can", the last "can" and the "can" after 7 were all one and the same as "Can" is not the same as "can". When you fixed that, the first "can" changed to the 6th position.

  15. #15
    Teny is offline Member
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    so the output are numbers and not words from the einsteinQuote?

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

    Default

    Yes, that is correct. indexOf finds the index number of the String you are searching for in the String being searched. Think of the main String as an array of chars and index of finds the array position of where the parameter String starts.

  17. #17
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Check the String API, it explains those string functions better.
    Tshegofatso Manakana
    a.k.a Untouchable

  18. #18
    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 Tshegofatsom View Post
    Check the String API, it explains those string functions better.
    Yep, I should have said this sooner. Thanks Tshegofatsom. The link is here:
    String (Java Platform SE 6)
    String (Java Platform SE 6)
    String (Java Platform SE 6)

Similar Threads

  1. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 12:22 AM
  2. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  3. Is my Pseudocode correct?
    By Clemenza1983 in forum New To Java
    Replies: 0
    Last Post: 01-29-2008, 04:07 AM
  4. Move to correct forum...
    By Winarto in forum Introductions
    Replies: 3
    Last Post: 01-18-2008, 03:00 AM
  5. To correct forum
    By Jman in forum Introductions
    Replies: 3
    Last Post: 01-18-2008, 02:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •