In this section if you set the string
String string1 = "Hi";
String string2 = new String("Hi");
then again it will print : "The strings are unequal."
just because of the reason that "==" operator compares two String objects, not their values, and both the objects (i.e. string1 & string2 are different by name and their address location), if we want to compare both the strings then we have to use equals() method as:
public class stringmethod{
public static void main(String[] args){
String string1 = "Hi";
String string2 = new String("Hi");
if (string1.equals(string2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}
}
}
How to Gather Whole String given at the time of enters string as input by using Scanner object??????????
For Example
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter a String");
String s=sc.next();
Output
Please Enter a String
Hi how are you?
But it takes only "Hi". I need to take whole the String. How??????????
help me with this, dont know which line being the problem.. totally the output not show up..
import java.io.*;
public class G2 {
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String gender,res;
System.out.println("Enter lines of text.");
gender = br.readLine();
String f= "Female";
String m="Male";
if(gender == m){
System.out.println("male");
} else if(gender == f){
System.out.println("female");
}
}
}
String Comparison by equals method.Pankaj September 27, 2011 at 11:52 PM
In this section if you set the string String string1 = "Hi"; String string2 = new String("Hi"); then again it will print : "The strings are unequal." just because of the reason that "==" operator compares two String objects, not their values, and both the objects (i.e. string1 & string2 are different by name and their address location), if we want to compare both the strings then we have to use equals() method as: public class stringmethod{ public static void main(String[] args){ String string1 = "Hi"; String string2 = new String("Hi"); if (string1.equals(string2)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } } }
*suvatea October 10, 2011 at 6:24 AM
this is not correct for reading strings from keyboard
ProgramesB.Rajesh February 22, 2012 at 11:35 AM
Hi Sir i want to compare to Two Strings .out put will be comes below s1=abc s2=abq ans:s3=aabbcg. this type of examples iwant Sir please
java stringsazhar April 18, 2012 at 2:10 PM
please give me the complete java strings with examples to prepare for an interview
Clarification About StringsDIVI HARI BABU CHOWDARY April 30, 2012 at 9:19 PM
How to Gather Whole String given at the time of enters string as input by using Scanner object?????????? For Example Scanner sc=new Scanner(System.in); System.out.println("Please Enter a String"); String s=sc.next(); Output Please Enter a String Hi how are you? But it takes only "Hi". I need to take whole the String. How??????????
StringsHemanth June 23, 2012 at 10:18 AM
strings are clearly understandable when we visit RoseIndia website thanks to rose india
can u help me with this~eppy azwan August 13, 2012 at 1:53 PM
help me with this, dont know which line being the problem.. totally the output not show up.. import java.io.*; public class G2 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String gender,res; System.out.println("Enter lines of text."); gender = br.readLine(); String f= "Female"; String m="Male"; if(gender == m){ System.out.println("male"); } else if(gender == f){ System.out.println("female"); } } }
Post your Comment