Why i can't  compare two string

* @(#)Assignment.java 1.0 03/12/12
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
//package myprojects.assignment;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.text.*;
* MainForm.java
* Created on 12 December 2003, 16:27
* @author Akasha
public class Assignment extends javax.swing.JFrame {
     private String myname;
private String utxtname,ptxtpass;
/** Creates new form MainForm */
public Assignment() {
initComponents();
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
private void initComponents() {
     //     JText = new JFormattedTextField();
     jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
getContentPane().setLayout(null);
          getContentPane().setBounds(800,800,800,800);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
jButton1.setBackground(new java.awt.Color(0, 0, 255));
jButton1.setForeground(new java.awt.Color(204, 255, 255));
jButton1.setText("Login");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
getContentPane().add(jButton1);
jButton1.setBounds(110, 160, 70, 30);
getContentPane().add(jTextField1);
jTextField1.setBounds(220, 70, 150, 20);
getContentPane().add(jTextField2);
jTextField2.setBounds(220, 100, 150, 20);
     //     getContentPane().add(JFormattedTextField1);
          //JFormattedTextField.
jLabel1.setText("User Name");
getContentPane().add(jLabel1);
jLabel1.setBounds(60, 70, 150, 16);
jLabel2.setText("Password");
getContentPane().add(jLabel2);
jLabel2.setBounds(60, 100, 160, 16);
pack();
private void formMouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
     myname="mazhar";
     utxtname = jTextField1.getText();
     if (myname == utxtname)
               JOptionPane.showMessageDialog(null,"This is"+ utxtname + myname+ " first ","Welcome",JOptionPane.INFORMATION_MESSAGE);
          else if (myname != utxtname)
               JOptionPane.showMessageDialog(null,"This is"+ myname+ "first action","Welcome",JOptionPane.INFORMATION_MESSAGE);
// Add your handling code here
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
* @param args the command line arguments
public static void main(String args[]) {
     Assignment assign =new Assignment();
     //width and hight
     assign.setSize(400, 175);
assign.show();
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

Basic java. I think that you need to read the tutorial... ;-) http://java.sun.com/docs/books/tutorial/
BAD (yours): you are comparing objects (ref to objects -> always differents even if there contains the same thing)
==========
if (myname == utxtname) {
JOptionPane.showMessageDialog(null,"This is"+ utxtname + myname+ " first ","Welcome",JOptionPane.INFORMATION_MESSAGE);
} else if (myname != utxtname) {
JOptionPane.showMessageDialog(null,"This is"+ myname+ "first action","Welcome",JOptionPane.INFORMATION_MESSAGE);
GOOD (mine):
===========
if (myname.equals(utxtname)) {
JOptionPane.showMessageDialog(null,"This is"+ utxtname + myname+ " first ","Welcome",JOptionPane.INFORMATION_MESSAGE);
} else {
//if (!myname.equals(utxtname)) {
JOptionPane.showMessageDialog(null,"This is"+ myname+ "first action","Welcome",JOptionPane.INFORMATION_MESSAGE);

Similar Messages

  • Compare two string in different line in textarea

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
                        public TextCounterPanel() {
                        setBackground(Color.DARK_GRAY);
                        textInput = new JTextArea();
                        textInput.setBackground(Color.WHITE);
                        JPanel south = new JPanel();
                        south.setBackground(Color.DARK_GRAY);
                        south.setLayout( new GridLayout(4,1,2,2) );
                             JButton countButton = new JButton("Process the Text");
                             countButton.addActionListener( new ActionListener() {
                                  public void actionPerformed(ActionEvent evt) {
                                       processInput();
                             south.add(countButton);
                        lineCountLabel = new JLabel(" Number of lines:");
                        lineCountLabel.setBackground(Color.WHITE);
                        lineCountLabel.setForeground(Color.BLUE);
                        lineCountLabel.setOpaque(true);
                        south.add(lineCountLabel);
                        setLayout( new BorderLayout(2,2) );
                        setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
                        JScrollPane scroller = new JScrollPane( textInput );
                        add(scroller, BorderLayout.CENTER);
                        add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel
    How can I compare two string in different line in text area

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
    public TextCounterPanel() {
    setBackground(Color.DARK_GRAY);
    textInput = new JTextArea();
    textInput.setBackground(Color.WHITE);
    JPanel south = new JPanel();
    south.setBackground(Color.DARK_GRAY);
    south.setLayout( new GridLayout(4,1,2,2) );
    JButton countButton = new JButton("Process the Text");
    countButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    processInput();
    south.add(countButton);
    lineCountLabel = new JLabel(" Number of lines:");
    lineCountLabel.setBackground(Color.WHITE);
    lineCountLabel.setForeground(Color.BLUE);
    lineCountLabel.setOpaque(true);
    south.add(lineCountLabel);
    setLayout( new BorderLayout(2,2) );
    setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    JScrollPane scroller = new JScrollPane( textInput );
    add(scroller, BorderLayout.CENTER);
    add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel

  • Compare two strings for partial or full match

    I have been trying to figure out away of comparing two strings.
    I can get the full string matching to successfully work but am struggling on how to get partial eg wildcard search working.
    say I had names like paul Duncan, George Morrison.
    I have split them up so its now paul , duncan i then use the users entered string to compare it against the name name to see if it matches anything in the first name or the last name if it does i added it to an arraylist.
    however i have tried t use a indexof as i believe this looks for similarity (Substring matches?) but it always returns -1.
    Could anyone possibly direct me to a method that will allow me to use partial text string to match it against stored user names.
    eg user enters pa
    I would return anything that would contain pa
    thanks for any help

    thank you for the reply
    Sorry I forgot to put in the code.
    This is a reduced down version including the read in from a text file as well as the index of if statement and the adding of it to an array list.
    public class FindContact {
    String res;
    /** Creates a new instance of FindContact */
    public FindContact() {
    public Object[] FindAUser(String passSearch) {
    System.out.println("getting here");
    ArrayList found = new ArrayList();
    String patternStr = ",";
    int j, i = 0;
    try {
    BufferedReader in = new BufferedReader(new FileReader("H:\\out.txt"));
    String str;
    while ((str = in.readLine()) != null) {
    String[] fields = str.split(patternStr);
    //for(j=0;j < fields.length;j++) {
    if (fields.length != 0) {
    if (passSearch.indexOf(fields[0])!=-1 || passSearch.indexOf(fields[1])!=-1) {
    found.add(str);
    System.out.println("ENDDDDDDDD value ");
    in.close();
    } catch (IOException e) {
    System.out.println(e.toString());
    Object[] foundResult = new Object[found.size()];
    foundResult = found.toArray();
    if (foundResult.length >= 0) {
    System.out.println(foundResult.length);
    return foundResult;
    Hope that helps as it has confused me and am not sure why it is not returning a value.
    Should I maybe use the contain method instead to search for the user inputted string?

  • Compare two strings in a formula

    Hi,
    I'd like to know if there's a way to compare two strings in a formula ?
    I have this consition:
    IF(@SUBSTRING(@NAME(@CURRMBR(TIME_FROM)),5)==@NAME(@CURRMBR(TIME)))
    But the == operator can only compare two data.
    Thanks,
    Cyril

    Hi,
    thank you both for your answers.
    When I figured that the == operator was for data only, I didn't even try to compare member names.
    I finally used the solution that Cameron suggested in another conversation. I created a date measure, and loaded some data since the beginning of my time to the end.
    It starts in 1974 (annually), then becomes monthly data, and then daily data from 2009, so it's not so many blocks to br created. In the end I have the same value for "from 2008"/"from Apr 30", and I can easily compare my dates.
    Note that I could have also used the @TODATE function, which turns a date into a number, but the format of date is limited and doesn't fit the format of my member names, so I got lasy... and I'm not sure my users would have apreciated that ;)
    Thanks, this post is solved.
    Cyril

  • How can I compare two summary field in cross-tab?

    <p>Dear expert:</p><p>I have one question for how can I compare two summary field in cross-tab?  I have following cross-table:</p><p>Type          Sector1     Sector2    Sector3       Total </p><p>Outlook         10            11           9              30         </p><p>Target            5              3           1               9</p><p>I want to compare the summary field(total) relationship percent, I want to get "9/30". Someone told me I must create the DB view or table via SQL, then can implete in Crystal Report. Can I implete it in Crystal Report via fomula or other function?</p><p>Thanks so much for your warm-hearted help!</p><p>Steven</p>

    Hello Steven, yes you can compare summary fields, If you are comparing Summary to Target, or vice versa - you can do it within Crystal Reports.
    1. In Suppress conditional formula, create 2 Global variables: CurrentOutlook and CurrentTarger and get the current value.
    2. In Display String formula for Total show ToText(CurrentTarget/CurrentOUtlook) + "%".
    For more difficult cases of compariing fields in cross--tab, you may look into http://www.relasoft.net/KB10001.html.
    Best,
    Alexander

  • Compare two strings and the save the greater

    To devolop a vi,it can compare two strings.A string is gived,the other is saved previously.If greater,then save,or do nothing.

    Hi,
    I am assuming by "greater" you mean the string length.
    Use the string length function from the strings palette, with shift-registers on a while loop. Write to the shift-register the "greater" string.
    I am attaching a diagram screenshot for this. NOTE: depending on how you want this VI to run, you can move the input and output string control and indicator inside our outside the while loop.
    Hope this is what you were looking for.
    Khalid
    Attachments:
    strlen.gif ‏17 KB

  • How to compare two strings whether both are equal while ignoring the difference in special characters (example: & vs & and many others)?

    I attempted to compare two strings whether they are equal or not. They should return true if both are equal.
    One string is based on Taxonomy's Term (i.e. Term.Name) whereas other string is based on String object.
    The problem is that both strings which seem equal return false instead of true. Both string values have different special characters though their special characters are & and &
    Snapshot of different design & same symbols:
    Is it due to different culture or language?
    How to compare two strings whether both are equal while ignoring the difference in special characters (& vs &)?

    Hi Jerioon,
    If you have a list of possible ambiguous characters the job is going to be easy and if (& vs &) are the only charracters in concern awesome.
    You can use the below solution.
    Before comparing pass the variables through a replace function to standarize the char set.
    $Var = Replace($Var,"&","&")
    This is going to make sure you don't end up with ambiguous characters failing the comparison and all the char are "&" in this case.
    Similar technique is used to ignore Character Cases 'a' vs. 'A'
    Regards,
    Satyajit
    Please “Vote As Helpful”
    if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

  • How to Compare two strings in PL/SQL

    Hi All,
    I need to compare two strings whether they are equal or not in PL/SQL.Is there any function to comparing the strings.

    Yes, the = sign.
    Are you after something like:
    IF v_string1 = v_string2 THEN
    ELSE
    END IF;?
    Edited by: Boneist on 27-Aug-2009 11:41

  • How can i compare two excel files with different no. of records.

    Hi
    I am on to a small project that involves us to compare two excel files. i am able to do it but am struck up at a point. When i compare 2 different .csv files with different no. of lines i am only able to compare upto a point till when the number of lines is same in both the files.
    Eg. if source file has 8 lines and target file has 12 lines. The difference is displayed only till 8 lines and the remaining 4 lines in source lines are not shown.
    Can you help me in displaying those extra 4 lines in source file. I am attaching my code snippet below..
    while (((strLine = br.readLine()) != null) && ((strLine1 = br1.readLine())) != null)
                     String delims = "[;,\t,,,|]";
                    String[] tokens = strLine.split(delims);
                    String[] tokens1 = strLine1.split(delims);
                   if (tokens.length > tokens1.length)
                    for (int i = 0; i < tokens.length; i++) {
                        try {
                            if (!tokens.equals(tokens1[i])) {
    System.out.println(tokens[i] + "<----->" + tokens1[i]);
    out.write(sno + " \t" + lineNo1 + " \t\t" + tokens[i] + "\t\t\t\t" + tokens1[i]);
    out.println();
    sno++;
    } catch (Exception exception)
    out.write(sno + " \t" + lineNo1 + " \t\t" + tokens[i] + "\t\t\t\t" + "");
    out.println();
    Thanks & Regards                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    A CSV file is not an Excel file.
    But apart from that your logic makes no sense.
    If the 2 files are of different sizes the files are different by definition, so further comparison isn't needed, you're done.
    If you want to compare individual records, you need to compare all records from one file with all records from the other, unless the order of records is important in which case your current system might work.
    That system however is overly complicated for comparing CSV files.
    As you assume a single record per line, and if one can assume those records to have identical layout (so no leading or trailing whitespace in or between columns in one file that's not in the other) comparing records is simply a matter of comparing the entire lines.

  • Comparing two strings

    How do you check to see if two strings have dashes in the same places?
    For example:
    String one= "dfsdt-ra-s-dsfd";
    String two= "dsfst-da-s-dsfd";
    would return true.
    String one="jhsdjfk-dsf-sdf"
    String two="d-dsf-dfsf-f"
    would return false.
    This is what i have:
    if (one.indexOf("-") == two.indexOf("-")) {
    return true;
    The problem with this if statement is it only checks the first time the dash come and not the whole string.
    How can I check the whole string to compare the index of string one and string two every time the dash come up.

    Hey,
    Check this code out, compile it and you should get it. Let me know if you have any questions.
    Thanks
    Riz
    public class StringCheck {
         String s1;
         String s2;
         boolean AreEqual;
         int lens1,lens2;
         public StringCheck(){
              s1="dfsdt-ra-s";
              s2="dfsdt-ra-s";
              AreEqual=false;
              lens1=s1.length();
              lens2=s2.length();
         public boolean CheckTheString(){
              boolean localcheck=false;
              if(lens1==lens2){
                   for(int i=0;i<lens1;i++){
                        if(s1.charAt(i)==s2.charAt(i)){
                             localcheck=true;
                             i++;
                        if(s1.charAt(i)!=s2.charAt(i)){
                             return false;
              if(localcheck)return true;
              else return false;
              //return true;
         public static void main(String args[]){
              StringCheck c = new StringCheck();
              boolean validate=c.CheckTheString();
              if(validate)
                   System.out.println("The string are equal");
              else
                   System.out.println("The String are not equal");
    }

  • How can I compare two collections at the same time? (View two grid views)

    I have two collections containing some of the same images. (My Nikon D70 did not put an end-of-file on some images. I recovered them into a different collection.) Now I want to display both collections side-by-side in grid view. I will select those images in the "recovered" collection that correspond to the bad images in the "main" collection, add the ratings etc, and move just these to another collection.
    It is extremely frustrating to have to bounce back and forth between collections, remembering each image one by one and selecting it in the "recovered" collection. (The image names are not preserved in the "recovered" collection -- I have to go by what the image looks like.)
    LightRoom allows me to compare photos in the compare view. I want to compare collections in two grid views.

    CaptureTheLight,
    you have ran into a situation when you have to compare two sets of images and now you're wondering how come Lightroom doesn't have such "obviously necessary" functionality? But you have to admit it, this is not such a common situation in a photographer's workflow recovers broken files and tries to compare them against themselves. I think it's a pretty specific feature you need. Still, Lightroom has enough powerful tools for editing and sorting images.
    For example...
    You could just put them all - "main" and "recovered" - into a single collection or into the Quick Collection. Label the entire "recovered" collection with, say, red and sort by capture time. Now you'll have everything side by side, ordered chronologically. The "recovered" images will stay next to the "main" images since their capture time will be the same, and they will also stand out since they have the red label.
    Make the thumbnails bigger and set up the grid view so it tints the thumbnail cell are tinted with the label color. Now, you can go quickly through them visually checking labeled vs unlabeled.

  • How can i compare two color images in vision builder for AI?

    What i want to do is to compare two images. I have a base color image that represents the desired colors and tones. I have another image to be compared to the base image. What i want to do is to compare this two images to know how close they are regarding contents of color and tones. In other words, i want to know how close is image 2 to image 1 (base sample)....
    I would like to know how to get the content of certain colors in an image and then compare this values with the same values from another image.
    For example..i have two sheets of paper that contain various mixed colors...i want to know the amount of green, red an blue in each image and then comapre this values.
    What i want to do is to compare difer
    ent samples of fabrics. this fabrics must be of a specified color...but due to the process they may vary in tone or even color...so i want to compare this fabrics to a amster sample to see how close they are in color and tone..
    Anything would help since i dont have experience in this type of comparisons...thanks

    VBAI allows you to work with grayscale images only. You can acquire an image, use the vision assistant to convert it grayscale by extracting the luminance plane (or any of the other color planes) and then analyze the resulting grayscale image. To do what you are talking about, though, it would really be better to get Vision for labview. You could then take color images, compare color plains, use statistical functions to determine average color values, and so on.

  • Compare two String

    String s1 = "ABC";
    String s2 = "ABC";
    *if (s1 == s2){*     //This tests true
    *if (s1.equals(s2)){*     //This tests true
    *if (s1.equalsIgnoreCase(s2)){*//This tests true
    My understanding is s1 refers one String object with the value ("ABC"); while s2 refers another String object with the value ("ABC"); So s1 and s2 actually refers to different objects.
    Is this correct?
    If so why the above test true?
    Thanks
    Scott

    Just to dig a little further...
    For
    String s1 = "ABC";
    and
    String s1 = new String("ABC");
    You mentioned:
    if((Object)s1 == (Object)s2){
    return true;
    should return false because the two String Object references s1 and s2 are stored at different memory locations*
    --My question, the fact that the comparision of ((Object)s1 == (Object)s2){... returning  false is:
    1) because they refer to different objects which are stored ar different locations?
    OR
    2) due to the above said: two String Object references s1 and s2 are stored at different memory locations
    --I prefer the 1).  But not so sure.
    --If answer is 2),  it will contradicts in following for s1 and s2 are stored at the different locations but the comparision ((Object)s1 == (Object)s2  will return true, i.e.,
    String s1 = "ABC";
    String s2 = "ABC";
    uses String.intern at compile time
    so
    s1 reference at aaa looks for the String object at xxx
    s2 reference at bbb looks for the String object at xxx
    this way
    if((Object)s1 == (Object)s2){
    return true;
    } should return true....
    Further,
    String s1 = "abc";
    String s3 = s1;
    Now, both s1 and s3 refer to the same Object ("abc"), but s1 and s3 are stored at different locations. Yet,
    if (s1 == s3)... will return true.
    This further indicates that the comparision of two references only has something to do with the actual location of the object, not the location of the reference itself.
    Like to get your opinion on this issue.
    Thanks
    Scott

  • Compare two strings in an array

    Hello!
    Please feel free to give me hints, but do not give me any code.
    Ok, here is what I am trying to do, I whant to compare my input string whith strings allready stored in the array. If my input string equals any of the allready excisting strings, a error wil show upp, otherwise store the new string in next avalibal position. I do not seem to get the method right for comparing the strings. here is the method I have written so far, please take a look and give me a hint, but no code. :)
    //Check if a user already excists     
    public void compareNames (People in_array [], String in_name) {
              for (int i=0; i<value.peopleCount; i++) {
                   if (in_name.equals(in_array.getPeople())) {
                   System.out.print("The user already excist, please chose another name");
    }value.peopleCount is an global count value for people arays.
    getPeople get the name from the people class.
    Martin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    A couple notes here.
    The name compareNames() is misleading if it is going to do what you described. A comparison will generally not have side-effects like outputting error messages to the console or adding new items to an array. It would be better if you called the method addIfNew(), and returned a boolean indicating whether the name was new or not. The caller would then be responsible for displaying an error message if the method returned false.
    I also suggest you use a List such as ArrayList instead of an array, otherwise you will have to resize and copy your array every time you add something to it that exceeds the array size, and will allow you to do away with the global peopleCount.

  • Compare two strings and verify they have same alphabets

    If i have two strings....s and t. I want to return whether s contains every letter in t. For example, "abcd" contains all the letters in "bbdc", since every letter in the second string appears in teh first.
    thanx

    If i have two strings....s and t. I want to return
    whether s contains every letter in t. For example,
    "abcd" contains all the letters in "bbdc", since every
    letter in the second string appears in teh first.This will do the job in s.length()*t.length() steps.
    //  Indicates whether s contains every letter in t.
    boolean allCharsContainedIn(String s, String t) {
        int len = t.length();
        for (int i = 0; i < len; i++) {
            if (s.indexOf(t.charAt(i) == -1)
                return false;
        return true;
    }S&oslash;ren

Maybe you are looking for