Results 1 to 6 of 6
  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Am I supposed to use the indexOf method?

    This is the homework problem word for word.

    (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 have already completed this problem and got it to run correctly using 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;
    			}				
    		}
    }
    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?

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

    Default 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

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default 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.

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

    Default Re: Am I supposed to use the indexOf method?

    Were there others who took your interpretation?

  5. #5
    Momar99 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default 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

  6. #6
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Am I supposed to use the indexOf method?

    @al_Marshy_1981 Yes I think so. I remember the teacher mentioned that a lot of people got this problem wrong before she handed back the homework.

    @Momar99 Ok, I appreciate your response, but how about you stop trolling.

Similar Threads

  1. Printing indexOf line
    By clrgomes in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 09:30 AM
  2. small method isn't supposed to go under 0
    By senca in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 02:23 PM
  3. Replies: 1
    Last Post: 04-23-2010, 06:56 PM
  4. String indexOf
    By ras_pari in forum Advanced Java
    Replies: 3
    Last Post: 10-07-2009, 08:33 AM
  5. Help regarding indexOf
    By gauravj in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 02:12 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
  •