Results 1 to 4 of 4
- 04-30-2011, 11:45 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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:
Could someone offer up a clue as to what I'm not getting?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()); }
- 04-30-2011, 11:49 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
You're using == to compare strings. Don't do this, because it compares object references instead of content. Use .equals() instead.
- 04-30-2011, 11:53 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Thanks for the quick response. I'll try what you said Iron Lion. My result is 0 which is incorrect.
- 04-30-2011, 12:01 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to populate a List within a Map
By elliotHenry in forum New To JavaReplies: 14Last Post: 03-29-2011, 08:25 PM -
Populate an array dynamically
By mmarkym in forum New To JavaReplies: 11Last Post: 12-31-2010, 03:31 PM -
"not a statement" error to array declaration
By SpaceMonkey in forum New To JavaReplies: 3Last Post: 11-25-2010, 11:01 PM -
Ripping apart a array list to populate a vector
By Adrien in forum AWT / SwingReplies: 0Last Post: 03-07-2010, 09:55 PM -
Help Pls!! Jcombobox populate with mysql
By kwink in forum AWT / SwingReplies: 1Last Post: 03-23-2009, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks