Solved. Need to keep my code hidden against being copied
Printable View
Solved. Need to keep my code hidden against being copied
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.
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
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
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.
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