Results 1 to 11 of 11
- 01-30-2012, 09:31 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
traditional for loops vs enhanced for loop help
Hello, I am in college and I am just starting to learn java after learning C/C++. I am writing a program that will take in a given string and replace certain letters with a different character or letter. I am used to for loops in C and have no problems with them, but in the spirit of trying to learn java and it just seeming better all the way around I would like to use enhanced for loops. I thought I understood the concept and then went to apply it to my code and ran into an error I can't seem to fix.
This is one method of a multiple method program:
as it stands now "for (int i: tobetrans)" generates a Type mismatch: cannot convert from element type String to int error. Yet if I put inJava Code:public static String translate(String str) { String[] tobetrans={"A","a","B","C","D","E","G","g","H","I","i","L","O","R","S","T","t","X","Z"}; String[] totransto={"4","@","8","(","|)","E","G","g","H","I","i","L","O","R","S","T","t","X","Z"}; for(int i:tobetrans){ str.contains(tobetrans[i]); // code to be added here to replace the tobetrans string to the totransto string } return str;
I get no error. Am I doing something wrong or am I just not understanding the concept of an enhanced for loop?Java Code:for (int i=0; i < tobetrans.length; i++)
Thanks in advance for the help.
- 01-30-2012, 09:54 AM #2
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: traditional for loops vs enhanced for loop help
If you say it in english, you're saying: "for every int 'i' in 'tobetrans'". But your String array is not filled with ints, it's filled with Strings. Hopefully that helps.
- 01-30-2012, 08:50 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: traditional for loops vs enhanced for loop help
Yeah that does help a little, so the enhanced for loop is slightly different than the other declaration? What I would like to do in the situation is to write something to the effect of for each element in tobetrans for the entire array tobetrans. Is that possible?
-
Re: traditional for loops vs enhanced for loop help
A for each loop has no explicit index, so your use of an int confused the compiler which expected a String since that's what the array holds. I don't understand the second part of your question, but if you check out the java tutorial on for-each loops, it will explain quite well how to use it.
- 01-31-2012, 01:24 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: traditional for loops vs enhanced for loop help
I've been reading up on for each loops, the problem I'm having is that I am confused about how to apply it to the specific situation. I don't really understand why as Bestsanchez said the compiler is looking for ints and not strings in the situation. Perhaps I should as a better question is it possible given my original example to make it into a for each loop?
-
Re: traditional for loops vs enhanced for loop help
- 01-31-2012, 10:10 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: traditional for loops vs enhanced for loop help
Because you have two arrays and you want to map from one to the other, ie the index of one maps to the index of the other, then an enhanced for-loop makes no sense. They use iterators generally, so the index is not available to you.
You have two possibilities, have a single array of Translation objects which contain an original character and a translation character, then loop through that (no need for indexes). Or use a Map<Character, Character> and loop over the entrySet.
- 02-01-2012, 07:20 AM #8
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Re: traditional for loops vs enhanced for loop help
You can do either by defining an index outside for the loop or as below
Method 1 :
Java Code:for (int j = 0; j < totransto.length; j++) { if(str.contains(tobetrans[j])){ str = str.replaceAll(tobetrans[j], totransto[j]); } }
Method 2
Java Code:int index = 0; for (String tempStr : tobetrans) { if(str.contains(tempStr)){ str = str.replaceAll(tempStr, totransto[index]); } index++; }
- 02-01-2012, 10:10 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: traditional for loops vs enhanced for loop help
I'd just do a replaceAll on the characters, and not bother with the contains().
They both search the string, so no point doing an extra search.
- 02-02-2012, 12:30 AM #10
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: traditional for loops vs enhanced for loop help
Thank you all for all the responses, I am still a little unsure of the limitations of the for each loop but I understand a lot better now and I ended up finishing my program using a method like bams Method 1 except i used str.replace instead :)
- 02-02-2012, 11:09 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
enhanced for loop
By billq in forum New To JavaReplies: 1Last Post: 05-01-2010, 02:07 PM -
Enhanced For Loop
By terahawks in forum New To JavaReplies: 3Last Post: 04-16-2010, 08:46 AM -
Help for Enhanced for loop
By jboy in forum New To JavaReplies: 6Last Post: 09-13-2009, 06:45 AM -
Iterating through ArrayList - traditional for loop
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 03:22 PM -
Enhanced for loop
By Java Tip in forum Java TipReplies: 0Last Post: 11-03-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks