Results 1 to 5 of 5
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default indexOf(String str) question:

    Line 23 is failing and I am not to sure why. I made a method and I am trying to use it in main. Simple task but it is saying I need to initialize it but if I initialize it to null it fails too.

    Java Code:
    import java.util.*; 
    
    public class ParseInput 
    {
    	String myline = "Learning Java is fun. Java is portable";
    	
    	int findPosition(String str)
    	{
    		int a = myline.indexOf(str); //finds position of a word return the int position or -1 if not found
    		
    		return a;
    	}
    	
    public static void main(String[] args)
    {
    	Scanner input = new Scanner(System.in);
    	System.out.println("What word would you like to find"); //prompt statement to user
    	String word=input.next(); //user input
    	
    	ParseInput v;
    	
    	int result;
    	result=v.findPosition(word);
    	
    	input.close();
    	
    	if(result >= 0)
    	{ System.out.println("The position of is located at "+result);}
    	else
    	{System.out.println("The word is not in the sentence."); }
    }
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: indexOf(String str) question:

    You need to initialize your v variable to be an instance of the ParseInput class. You do that using the new keyword.

    Java Code:
    ParseInput v = new ParseInput();
    This is exactly what you're doing with your Scanner variable: initializing it to an instance of a class using the new keyword before you use it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: indexOf(String str) question:

    What error do you get exactly? Could you provide an example input that fails? Don't forget, java is case sensitive.

  4. #4
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: indexOf(String str) question:

    Thank you, wow do I feel like a boob right now.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: indexOf(String str) question:

    Happens to all of us. Keep coding :D

Similar Threads

  1. IndexOf()?
    By Jeron kahyar in forum New To Java
    Replies: 3
    Last Post: 10-15-2012, 08:27 PM
  2. Help with indexof please...
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 02-07-2012, 10:36 PM
  3. Replies: 2
    Last Post: 06-10-2011, 08:15 AM
  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
  •