Results 1 to 5 of 5
- 02-06-2011, 12:23 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
- 02-06-2011, 01:36 PM #2
declare a string array for example
String[] response = new String[10];
and inside your while-loop where you call the hasNextLine() you can do
response[i] = scan.nextLine();
i++;
please instantiate and int i outside the try/catch-block with 0.Last edited by j2me64; 02-06-2011 at 01:40 PM.
- 02-06-2011, 02:01 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Thank you so much, it's worked.
I hate to be a pain but using that same file, when a user inputs a word which contains "network" for example in another class that is linked to the responder,
So when the user enters a word that is in the data text file, print out a specific line in the file which relates to it.
So if I entered network for example, it would look at the text file and return "Please make sure you have typed in the correct Network Security Key." because it has the word network in it?
Thank you
- 02-06-2011, 02:28 PM #4
the class String has a method called contains(CharSequence s) that returns true if the charsequence given as s is in the String the method is called, example
Java Code:String s = "My name is Bond, James Bond"; System.out.println(s.toUpperCase().contains("BOND"));
note, that chained methods are evaluated from left to right, so the string s is set to uppercases so that the search is not case sensitif (if you want that). for your code loop through the arrays of string a make a call to contains() to look for your word.Last edited by j2me64; 02-06-2011 at 02:31 PM.
- 02-06-2011, 02:46 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Mmmm, but wouldn't that require me to write out all the different sentences?
I want to simply scan a text file for a specific word that the user has input and then system.out the sentence that contains the word.
while(!finished) {
String input = reader.getInput();
if (input.contains("stupid")) {
swearing = true;
System.out.println("I'm sorry, but that is an offence word. Please improve your language.");
}
else if(input.startsWith("bye")) {
finished = true;
printGoodbye();
}
else {
String response = responder.generateTextResponse();
System.out.println(response);
I use this class to generate the response, so if the user inputs a certain word, how can it scan the text file and system.out the sentence containing said word.
Sorry, I'm very Java illiterate lol
Similar Threads
-
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
creating a graph from a text file
By chinolee in forum New To JavaReplies: 3Last Post: 11-23-2009, 08:36 AM -
Creating&Saving as a different Text file
By right2001 in forum New To JavaReplies: 2Last Post: 04-07-2009, 07:17 AM -
Creating a Data Structure to Hold an Array and Distance Value
By rdoane in forum New To JavaReplies: 11Last Post: 04-01-2009, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks