java - Formal Greeting app only displays last if statement can't figure out why -
package chapter6; import java.util.scanner; public class formal_greeting { public static void main(string[] args) { string title; string name; string mr = null; string miss = null; string ms = null; string mrs = null; scanner input = new scanner(system.in); system.out.print("enter name: "); name = input.next(); system.out.print("enter title: "); title = input.next(); if (title.equalsignorecase(mr)) { system.out.println("hello sir"); }else if (title.equalsignorecase(miss)) { system.out.println("hello ma'am"); }else if (title.equalsignorecase(ms)) { system.out.println("hello ma'am"); }else if (title.equalsignorecase(mrs)) { system.out.println("hello ma'am"); }else { system.out.println("hello " + name); } } }
i can't figure out why if statements won't work. far displays (hello "name") can please tell me wrong if statement???
problem not in if statements.change method input.next()
input.nextline()
.because using next()
entered before space. if use nextline()
scanner read entire input line.
for more reference click here.
hope helps