little help with this substring


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: little help with this substring

  1. #1
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150

    little help with this substring

    Hi guys I have the following code the gets executed when a button is clicked. Basiclly i am trying to read a string, character by character using the substring method.

    I am trying to compare the individual character in the string with a character. I have the code below but its not working. What seems to happen is that my vowel counter is incrementing when there is no match. The if statment should return false if no match is found. I have a text field. If i enter ten characters, then the loop will be executed ten times and each time it will say a match has been found,, when no match has really been found. Can any one help?

    Code:
    public void actionPerformed( ActionEvent e )
    		{
    		String singleChar="";
    		String wholeString = fieldText.getText();
    		int TotalChars = wholeString.length();
    		int vowelCount=0;
    		int i=0;
    		
    			while(i < TotalChars)
    				{
    				singleChar = wholeString.substring(i, i + 1);
    				
    					if (singleChar=="a");
    						{
    						vowelCount++; 
    						System.out.println("match");
    						}
    				
    
    		System.out.println(singleChar);
    		i++;
    				}
    		JOptionPane.showMessageDialog(null,"Total number of vowels: " + vowelCount);
    		}

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    1. You have a semi-colon after your if statement.
    2. You can't use == when comparing strings, that compares memory. You must use singleChar.equals("a");
    Code:
    if (singleChar=="a");
    So really, that's like:
    Code:
    if (singleChar == "a") {
          ;
    }
    So if singleChar did have the same memory location as "a" (which it wouldn't), no code would be executed!
    After that, it sees:
    Code:
    {
    	vowelCount++; 
    	System.out.println("match");
    }
    So it just goes right along and executes that.

    i wouldn't even do it the way you did . I would do it like this:
    Code:
    public void actionPerformed( ActionEvent e ) {
    	/** store the String value of fieldText into String str */
    	String str = fieldText.getText();
    	/** vowelCount will be used to count the vowels in str */
    	int vowelCount = 0;
    	
    	/** loop str.length() times */
    	for( int i = 0; i < str.length(); i++ ) {
    		/** 
    		 * take the character at the i position in str 
    		 * ( "hi".charAt( 0 ); would be 'h', "hi".charAt( 1 ); would be 'i' )
    		 */
    		char ch = str.charAt( i );
    		
    		/** switch can be used for ints and chars (which are basically the same thing) */
    		switch( ch ) {
    		/** if ch is 'a', 'e', 'i', 'o', or 'u', increase the vowelCount and print "match" */
    		case 'a':
    		case 'e':
    		case 'i':
    		case 'o':
    		case 'u':
    			vowelCount++; 
    			System.out.println( "match" );
    			break;
    		/** 
    		 * even though this does nothing, it's good to have
    		 * it in case you decide to do something like counting the consonants
    		 */
    		default:
    			break;
    		}
    	}
          /** display amount of vowels */
    	JOptionPane.showMessageDialog( null, "Total number of vowels: " + vowelCount );
    }
    Last edited by destin; 12-26-2005 at 09:08 PM.

Similar Threads

  1. Replies: 1
    Last Post: 01-04-2002, 10:25 AM
  2. finding substring in string
    By Bill Heffner in forum Web
    Replies: 2
    Last Post: 11-12-2001, 06:03 PM
  3. Replies: 3
    Last Post: 04-05-2001, 01:33 PM
  4. Replies: 1
    Last Post: 03-15-2001, 09:33 AM
  5. Replies: 0
    Last Post: 11-22-2000, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center