java - How can I read any user input from the scanner library? -


i'm new java, don't think idiot. anyways, i've been trying make program can read letter console , decide operation use, let's add. however, can't if loop read variable decides operator use, here code, , please help.

import java.util.scanner;   class main {    public static void main(string[] args) {        scanner user_input = new scanner( system.in );         int number;         string function;         system.out.println("what want do? (a add; s to" +         " subrtact; d divited; m multiply, , sq square nummber.)" );         function = user_input.next();         if (function == "sq"){             system.out.print("enter number: ");             number = user_input.nextint();             system.out.print(number * number);         } else {             system.out.println("unidentified function!");   } } } 

(i made description shorter fit).

this example started in right direction.

import java.util.scanner;  public class example {      public static void main(string[] args) {         scanner user_input = new scanner(system.in);         int num1, num2, result;          system.out.println("what want do? (a add; s to"                 + " subrtact; d divited; m multiply, , s square nummber.)");          string choice = user_input.next();          // add         if (character.isletter('a')) {             system.out.println("enter first number: ");             num1 = user_input.nextint();             system.out.println("enter second number: ");             num2 = user_input.nextint();              result = num1 + num2;             system.out.println("answer: " + result);         }     } } 

Comments