Results 1 to 18 of 18
Thread: Is this the correct Output?
- 04-12-2009, 09:31 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
Is this the correct Output?
so heres the code fragment.
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)".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"));
making sure if i am getting the right output or not.
- 04-13-2009, 01:03 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-13-2009, 01:23 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
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.
-
- 04-13-2009, 02:18 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
Sorry, this is what I got "343434" after i reran the .class
Did i script it right?
The Code fragment I got is from a PPT from the "Introduction to programming with Java" by John Dean chapter 5.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"));
}
}
-
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:
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") + "" //no space between quotes + einsteinQuote.indexOf("can", 7) + "" //no space between quotes + einsteinQuote.lastIndexOf("can"));
Good luck.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"));
- 04-13-2009, 03:13 AM #7
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
This is my script so far
and my output was, "6 34 34".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"));
}
}
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.
-
So, you've figured it all out, correct?
- 04-13-2009, 03:28 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
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?
-
no your code is correct and the output is correct.
this guy:
outputs the first "can" it finds which you've counted to be at position 6.Java Code:einsteinQuote.indexOf("can")
This guy:
finds the first "can" at position 7 or beyond, which you've counted out to be position 34,Java Code:einsteinQuote.indexOf("can", 7)
and this guy:
finds the location of the last "can" which is the same as the one above. It all works fine.Java Code:einsteinQuote.lastIndexOf("can"));
- 04-13-2009, 04:08 AM #11
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
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.
-
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.
I don't quite get you here.So the lastIndexOf(" ") is more of a place of where it should stop?
- 04-13-2009, 04:20 AM #13
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
Yeah I did count it.
Just to double check, what is the actual output?
-
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.
- 04-13-2009, 04:24 AM #15
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
so the output are numbers and not words from the einsteinQuote?
-
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.
- 04-13-2009, 09:00 AM #17
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Check the String API, it explains those string functions better.
Tshegofatso Manakana
a.k.a Untouchable ™
-
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
-
Output correct grammar
By JordashTalon in forum New To JavaReplies: 2Last Post: 03-06-2009, 12:22 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
Is my Pseudocode correct?
By Clemenza1983 in forum New To JavaReplies: 0Last Post: 01-29-2008, 04:07 AM -
Move to correct forum...
By Winarto in forum IntroductionsReplies: 3Last Post: 01-18-2008, 03:00 AM -
To correct forum
By Jman in forum IntroductionsReplies: 3Last Post: 01-18-2008, 02:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks