Im a beginner plz help to solve this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Im a beginner plz help to solve this

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String x=sc.nextLine(); if(x=hi){ System.out.println ("hi"); } else{ System.out.println ("do something"); } } }

28th Sep 2017, 9:56 AM
Abinesh N
Abinesh N - avatar
4 Answers
0
if(x.equals("hi")){ System.out.println ("hi"); } else{ System.out.println ("do something"); }
28th Sep 2017, 9:59 AM
Niush
Niush - avatar
+ 3
/* dont use == to compare strings use below statement so if your string is case sensitive it will be ignored but if you want it to be exactly "hi" then use @Niush statement */ if(x.equalsIgnoreCase("hi");
28th Sep 2017, 10:04 AM
D_Stark
D_Stark - avatar
+ 2
one more doubt if its integer then if (x=1) is correct or if (x .equals(1))
28th Sep 2017, 10:10 AM
Abinesh N
Abinesh N - avatar
+ 2
Comparing int use this > (x==1) Comparing String use this > (x.equals("hi"))
28th Sep 2017, 10:24 AM
D_Stark
D_Stark - avatar