Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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:

    Java 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;
    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 in

    Java Code:
    for (int i=0; i < tobetrans.length; i++)
    I get no error. Am I doing something wrong or am I just not understanding the concept of an enhanced for loop?

    Thanks in advance for the help.

  2. #2
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default 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.

  3. #3
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: traditional for loops vs enhanced for loop help

    Quote Originally Posted by RagingPenguin4 View Post
    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.
    No, he said the compiler wants you to use Strings not ints. Try it with a String type instead. Just try it. You won't break your computer.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default 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.

  8. #8
    bams is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default 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++;
    	    }

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default 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.

  10. #10
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default 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 :)

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: traditional for loops vs enhanced for loop help

    The enhanced for loop is a version of this structure:
    Java Code:
    for (Iterator<SomeClass> i = myList.iterator(); i.hasNext(); ) {
        SomeClass myObject = i.next();
    ... then do stuff
    }
    Which is why you don't have access to any indexes.

Similar Threads

  1. enhanced for loop
    By billq in forum New To Java
    Replies: 1
    Last Post: 05-01-2010, 02:07 PM
  2. Enhanced For Loop
    By terahawks in forum New To Java
    Replies: 3
    Last Post: 04-16-2010, 08:46 AM
  3. Help for Enhanced for loop
    By jboy in forum New To Java
    Replies: 6
    Last Post: 09-13-2009, 06:45 AM
  4. Replies: 0
    Last Post: 11-14-2007, 03:22 PM
  5. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 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
  •