Results 1 to 4 of 4
  1. #1
    MrJinx is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default using an if statement to populate an array

    I have an assigment to read through a text file and count the occurrence of a string. I thought I could populate an array with the matches and then show the size of the array, so I tried this:

    Java Code:
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    			  	String sourceFile = JOptionPane.showInputDialog("Enter the source file name: ");
    			
    			    
    			  	if(sourceFile!=null){
    			  	
    			    FileInputStream data= new FileInputStream(sourceFile);
    				BufferedReader data_in = new BufferedReader(new InputStreamReader(data));
    				
    				String str;
    
    				while( (str = data_in.readLine() ) != null){
    					if (str == "147628") {
    						numbers.add(Integer.parseInt(str));
    					    }
    
    					
    				}
    	    	
    		    JOptionPane.showMessageDialog(null, numbers.size());
    			  	}
    Could someone offer up a clue as to what I'm not getting?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    You're using == to compare strings. Don't do this, because it compares object references instead of content. Use .equals() instead.

  3. #3
    MrJinx is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the quick response. I'll try what you said Iron Lion. My result is 0 which is incorrect.

  4. #4
    MrJinx is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    :) I suppose I should have tried it before my last post but that was it. I wish I found this site last night, I'm embarressd to say how long I played with this. My last challenge is to list a range of numbers. Thanks again.

Similar Threads

  1. How to populate a List within a Map
    By elliotHenry in forum New To Java
    Replies: 14
    Last Post: 03-29-2011, 09:25 PM
  2. Populate an array dynamically
    By mmarkym in forum New To Java
    Replies: 11
    Last Post: 12-31-2010, 04:31 PM
  3. "not a statement" error to array declaration
    By SpaceMonkey in forum New To Java
    Replies: 3
    Last Post: 11-26-2010, 12:01 AM
  4. Ripping apart a array list to populate a vector
    By Adrien in forum AWT / Swing
    Replies: 0
    Last Post: 03-07-2010, 10:55 PM
  5. Help Pls!! Jcombobox populate with mysql
    By kwink in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •