Results 1 to 5 of 5
Thread: indexOf(String str) question:
- 04-09-2014, 07:32 PM #1
Senior Member
- Join Date
- Jan 2014
- Posts
- 137
- Rep Power
- 0
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."); } } }
- 04-09-2014, 07:35 PM #2
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();
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-09-2014, 07:36 PM #3
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.
- 04-09-2014, 07:46 PM #4
Senior Member
- Join Date
- Jan 2014
- Posts
- 137
- Rep Power
- 0
Re: indexOf(String str) question:
Thank you, wow do I feel like a boob right now.
- 04-09-2014, 07:59 PM #5
Similar Threads
-
IndexOf()?
By Jeron kahyar in forum New To JavaReplies: 3Last Post: 10-15-2012, 08:27 PM -
Help with indexof please...
By fatabass in forum New To JavaReplies: 7Last Post: 02-07-2012, 10:36 PM -
How to string.indexOf for multiple strings and return first instance?
By Arrowx7 in forum New To JavaReplies: 2Last Post: 06-10-2011, 08:15 AM -
String indexOf
By ras_pari in forum Advanced JavaReplies: 3Last Post: 10-07-2009, 08:33 AM -
Help regarding indexOf
By gauravj in forum New To JavaReplies: 1Last Post: 07-10-2007, 02:12 PM
Bookmarks