java - I am trying to compare strings, but it is not recognizing -
so trying check strings using if statement,i created test computer gives predetermined responses depending on input, inputs working happen hi or hello, strings spaces not seem work, why that?
import java.util.scanner; public class computabot { public static void main(string[] args) { scanner scan = new scanner(system.in); string text; string computer = "computabot:"; string[] input = { "how you?", "hi", "hello", "what name", "do chicken?", "where from?", "do cheese?" }; do{ system.out.println("type talk!:"); text = scan.next(); if (text.equals(input[1]) || text.equals(input[2])) { system.out.println(computer + " " +"hello!"); }else if (text.equalsignorecase(input[0])) { system.out.println(computer + " " +"i'm fine, you?!"); }else if (text.equals(input[3])) { system.out.println(computer + " " +"jimmy"); }else if (text.equals(input[4])) { system.out.println(computer + " " +"yes! love it"); }else if (text.equals(input[5])){ system.out.println(computer + " " +"germany"); }else if (text.equals(input[6])){ system.out.println(computer + " " +"only on pizza"); }else system.out.println("bye"); }while(!text.equals("bye")); scan.close(); } }
the method next()
reads 1 word. should use nextline()
reads whole line (delimited new-line character entered when press enter):
text = scan.nextline();
from javadocs:
public string next(): finds , returns next complete token scanner. complete token preceded , followed input matches delimiter pattern.
Comments
Post a Comment