Results 1 to 6 of 6
- 12-07-2011, 08:41 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Am I supposed to use the indexOf method?
This is the homework problem word for word.
I have already completed this problem and got it to run correctly using the indexOf method.(Checking substrings) You can check whether a string is a substring of another string by using the indexOf method in the String class. Write your own method for this function. Write a program that prompts the user to enter two strings, and check whether the first string is a substring of the second.
I turned this in last week. My teacher took off 10 points because I used the indexOf method. I believe the problem specifically asks to use the indexOf method and write a method for that function. Did I misunderstand the problem? Am I supposed to use the indexOf method?Java Code:import java.util.*; import java.lang.String; public class CheckingSubstring { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a String: "); String string1 = input.next(); System.out.print("Please enter a second String: "); String string2 = input.next(); if (isSubstring(string1, string2)) { System.out.println("The first string is a substring of the second."); } else { System.out.println("The first string is NOT a substring of the second."); } } public static boolean isSubstring(String string1, String string2) { int position; position = string2.indexOf(string1); if (position == -1) { return false; } else { return true; } } }
- 12-07-2011, 08:45 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Am I supposed to use the indexOf method?
Looks like the instructions said that it is possible using the indexOf method but then asks you to implement your own version of indexOf
- 12-07-2011, 09:51 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Re: Am I supposed to use the indexOf method?
Oh I see. It would have been better if they worded it differently like this:
Write a indexOf method that checks whether a string is a substring of another string. Write a test program that prompts the user to enter two strings, and check whether the first string is a substring of the second.
- 12-07-2011, 09:55 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Re: Am I supposed to use the indexOf method?
Were there others who took your interpretation?
- 12-07-2011, 09:57 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
Re: Am I supposed to use the indexOf method?
No from what i see i can see no reason what so ever to use the index it makes no sense what so ever how about you get good at java
- 12-07-2011, 10:01 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Printing indexOf line
By clrgomes in forum New To JavaReplies: 2Last Post: 03-06-2011, 08:30 AM -
small method isn't supposed to go under 0
By senca in forum New To JavaReplies: 2Last Post: 08-09-2010, 01:23 PM -
is this correct not supposed to edit the printValues method
By npm1 in forum Advanced JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
String indexOf
By ras_pari in forum Advanced JavaReplies: 3Last Post: 10-07-2009, 07:33 AM -
Help regarding indexOf
By gauravj in forum New To JavaReplies: 1Last Post: 07-10-2007, 01:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks