The Artima Developer Community
Sponsored Link

Java Answers Forum
String Comparison

1 reply on 1 page. Most recent reply: Mar 21, 2003 3:41 PM by Rich Burgis

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Craig

Posts: 4
Nickname: craig7200
Registered: Jan, 2003

String Comparison Posted: Mar 21, 2003 12:52 PM
Reply to this message Reply
Advertisement
Im stumped on this .. im trying to state whether the first string is less than, equal to or greater than the second while ignoring the case using regionMatches. The program creates a GUI in which a "compare it" button is clicked for results. Just looking for some assistance or suggestions on the comparison part.

Here is what I have so far:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StringCompare extends JFrame {
private JLabel prompt1, prompt2, prompt3, prompt4;
private JButton toCompare;
private JTextField input;

//constructor builds GUI

public StringCompare()
{
super( "String Comparison" );

Container container = getContentPane();
container.setLayout( new FlowLayout());

prompt1 = new JLabel( " Enter the first string ");
input = new JTextField( 15 );
container.add( prompt1 );
container.add( input );

prompt2 = new JLabel( " Enter the second string ");
input = new JTextField( 15 );
container.add( prompt2 );
container.add( input );

prompt3 = new JLabel( " Enter the number of characters ");
input = new JTextField( 10 );
container.add( prompt3 );
container.add( input );

prompt4 = new JLabel( " Enter starting index ");
input = new JTextField( 10 );
container.add( prompt4 );
container.add( input );

toCompare = new JButton("Compare it");
toCompare.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent actionEvent )
{
// test regionMatches (ignore case) STUMPED ON THIS PART

if (prompt1.regionMatches(true,

}
}
);
container.add ( toCompare );
setSize( 200, 285 );
setVisible ( true );
}

public static void main( String args[] )
{
StringCompare application = new StringCompare();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}


Rich Burgis

Posts: 17
Nickname: songbird
Registered: Mar, 2003

Re: String Comparison Posted: Mar 21, 2003 3:41 PM
Reply to this message Reply
Try (from the api javadoc)

compareToIgnoreCase


public int compareToIgnoreCase(String?str)


Compares two strings lexicographically, ignoring case considerations. This method returns an integer whose sign is that of this.toUpperCase().toLowerCase().compareTo( str.toUpperCase().toLowerCase()).

Parameters:
str - the String to be compared.
Returns:
a negative integer, zero, or a positive integer as the the specified String is greater than, equal to, or less than this String, ignoring case considerations.
Since:
1.2

It sounds like what you were looking for

Good luck
Rich

Flat View: This topic has 1 reply on 1 page
Topic: How to get obj address / actual reference value? Previous Topic   Next Topic Topic: Continuous Scrolling

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use