Results 1 to 8 of 8

Thread: function

  1. #1
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default function

    This function is supposed to transform the given string into a new one. This code works for every character except 'c'. It is not transforming this character like the others, but leaves it changeless. What's the matter? :S
    Java Code:
    	public String transfer_eng_geo_dummyway(String s) {
    		String result = new String();
    		int i = 0;
    		while(i<s.length()) {
    			if(s.charAt(i)=='a') result = new String(result+"ა"); else
    			if(s.charAt(i)=='b') result = new String(result+"ბ"); else
    			if(s.charAt(i)=='g') result = new String(result+"გ"); else
    			if(s.charAt(i)=='d') result = new String(result+"დ"); else
    			if(s.charAt(i)=='e') result = new String(result+"ე"); else
    			if(s.charAt(i)=='v') result = new String(result+"ვ"); else
    			if(s.charAt(i)=='z') result = new String(result+"ზ"); else
    			if(s.charAt(i)=='T') result = new String(result+"თ"); else
    			if(s.charAt(i)=='i') result = new String(result+"ი"); else
    			if(s.charAt(i)=='k') result = new String(result+"კ"); else
    			if(s.charAt(i)=='l') result = new String(result+"ლ"); else
    			if(s.charAt(i)=='m') result = new String(result+"მ"); else
    			if(s.charAt(i)=='n') result = new String(result+"ნ"); else
    			if(s.charAt(i)=='o') result = new String(result+"ო"); else
    			if(s.charAt(i)=='p') result = new String(result+"პ"); else
    			if(s.charAt(i)=='J') result = new String(result+"ჟ"); else
    			if(s.charAt(i)=='r') result = new String(result+"რ"); else
    			if(s.charAt(i)=='s') result = new String(result+"ს"); else
    			if(s.charAt(i)=='t') result = new String(result+"ტ"); else
    			if(s.charAt(i)=='u') result = new String(result+"უ"); else
    			if(s.charAt(i)=='f') result = new String(result+"ფ"); else
    			if(s.charAt(i)=='q') result = new String(result+"ქ"); else
    			if(s.charAt(i)=='R') result = new String(result+"ღ"); else
    			if(s.charAt(i)=='y') result = new String(result+"ყ"); else
    			if(s.charAt(i)=='S') result = new String(result+"შ"); else
    			if(s.charAt(i)=='C') result = new String(result+"ჩ"); else
    			if(s.charAt(i)=='с') result = new String(result+"ც"); else
    			if(s.charAt(i)=='Z') result = new String(result+"ძ"); else
    			if(s.charAt(i)=='w') result = new String(result+"წ"); else
    			if(s.charAt(i)=='W') result = new String(result+"ჭ"); else
    			if(s.charAt(i)=='x') result = new String(result+"ხ"); else
    			if(s.charAt(i)=='j') result = new String(result+"ჯ"); else
    			if(s.charAt(i)=='h') result = new String(result+"ჰ"); else
    								 result = new String(result+s.charAt(i));
    			i++;
    		}
    		return result;
    	}

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Hi there, you code seems to be fine so i don't think why doesn't the 'c' works. but i have a comment that may be a better approach for you program's efficiency:

    1) instead of reading s.charAt(i) it is better to initiate it, that is in the first line after you while condition it is better to say
    char letter = s.charAt(i);
    in that way you compare this letter every time you need it


    2) instead of the stating every time
    result = new String(result+"ჰ")
    you can use as well String charac = ""; at the beginning so you change this every time in the if statement and at the end you state
    result += charac
    this will really save time and effort for your program and will help others understand your code more


    3) i think it is better to use for(int i = starting point; i < last point; i++) that way you don't need to add the i at the end
    Last edited by baf06; 04-10-2011 at 09:49 AM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    please do these changes and try again to run your program and tell me what happens with you, hope i can help
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    instead of reading s.charAt(i) it is better to initiate it
    Thank you, now my code looks much better and is readable. But it still doesn't works :S

    Java Code:
    public String transfer_eng_geo_dummyway(String s) {
    		String result = new String();
    		int i = 0;
    		char l;
    		while(i<s.length()) {
    			l=s.charAt(i);
    			if(l=='a') result+="ა"; else
    			if(l=='b') result+="ბ"; else
    			if(l=='g') result+="გ"; else
    			if(l=='d') result+="დ"; else
    			if(l=='e') result+="ე"; else
    			if(l=='v') result+="ვ"; else
    			if(l=='z') result+="ზ"; else
    			if(l=='T') result+="თ"; else
    			if(l=='i') result+="ი"; else
    			if(l=='k') result+="კ"; else
    			if(l=='l') result+="ლ"; else
    			if(l=='m') result+="მ"; else
    			if(l=='n') result+="ნ"; else
    			if(l=='o') result+="ო"; else
    			if(l=='p') result+="პ"; else
    			if(l=='J') result+="ჟ"; else
    			if(l=='r') result+="რ"; else
    			if(l=='s') result+="ს"; else
    			if(l=='t') result+="ტ"; else
    			if(l=='u') result+="უ"; else
    			if(l=='f') result+="ფ"; else
    			if(l=='q') result+="ქ"; else
    			if(l=='R') result+="ღ"; else
    			if(l=='y') result+="ყ"; else
    			if(l=='S') result+="შ"; else
    			if(l=='C') result+="ჩ"; else
    			if(l=='с') result+="ც"; else
    			if(l=='Z') result+="ძ"; else
    			if(l=='w') result+="წ"; else
    			if(l=='W') result+="ჭ"; else
    			if(l=='x') result+="ხ"; else
    			if(l=='j') result+="ჯ"; else
    			if(l=='h') result+="ჰ"; else
    								 result+=l;
    			i++;
    		}
    		return result;
    	}

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by nikkka View Post
    Thank you, now my code looks much better and is readable. But it still doesn't works :S

    Java Code:
    public String transfer_eng_geo_dummyway(String s) {
    		String result = new String();
    		int i = 0;
    		char l;
    		while(i<s.length()) {
    			l=s.charAt(i);
    			if(l=='a') result+="ა"; else
    			if(l=='b') result+="ბ"; else
    			if(l=='g') result+="გ"; else
    			if(l=='d') result+="დ"; else
    			if(l=='e') result+="ე"; else
    			if(l=='v') result+="ვ"; else
    			if(l=='z') result+="ზ"; else
    			if(l=='T') result+="თ"; else
    			if(l=='i') result+="ი"; else
    			if(l=='k') result+="კ"; else
    			if(l=='l') result+="ლ"; else
    			if(l=='m') result+="მ"; else
    			if(l=='n') result+="ნ"; else
    			if(l=='o') result+="ო"; else
    			if(l=='p') result+="პ"; else
    			if(l=='J') result+="ჟ"; else
    			if(l=='r') result+="რ"; else
    			if(l=='s') result+="ს"; else
    			if(l=='t') result+="ტ"; else
    			if(l=='u') result+="უ"; else
    			if(l=='f') result+="ფ"; else
    			if(l=='q') result+="ქ"; else
    			if(l=='R') result+="ღ"; else
    			if(l=='y') result+="ყ"; else
    			if(l=='S') result+="შ"; else
    			if(l=='C') result+="ჩ"; else
    			if(l=='с') result+="ც"; else
    			if(l=='Z') result+="ძ"; else
    			if(l=='w') result+="წ"; else
    			if(l=='W') result+="ჭ"; else
    			if(l=='x') result+="ხ"; else
    			if(l=='j') result+="ჯ"; else
    			if(l=='h') result+="ჰ"; else
    								 result+=l;
    			i++;
    		}
    		return result;
    	}
    Donīt do it that way: repeatedly adding chars to a String is extremely inefficient; also testing for those chars in a humongous if-else chain is not the way to go; do it like this:

    Java Code:
    private static final String orig= "abg ...";
    private static final String trans= "აბგ ...";
    
    public static String translate(String s) {
       StringBuilder sb= new StringBuilder();
       for (int i= 0; i < s.length(); i++) {
          int j= orig.indexOf(s.charAt(i));
          sb.append((j >= 0)?trans.charAt(j):s.charAt(i));
       }
       return sb.toString();
    }
    The character at position i in String orig has to correspond the character i in String trans; if the character is not present in the String, no translation is attempted and the original character is used. The results are collected in a StringBuffer and the String form of it is returned.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    I tried your code and it didn't work at first, but i just erased the 'c' and wrote again and all of the sudden it worked perfectly, and Jos's comment is very helpful, and if you didn't really understand it you can set the strings orig and trans to be Arrays if you know how to use them, because what he opposed needs more practice and knowledge.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  7. #7
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Donīt do it that way: repeatedly adding chars to a String is extremely inefficient; also testing for those chars in a humongous if-else chain is not the way to go; do it like this:
    Thank you, your comment is very helpful :) I never considered this way of decoding :)
    I tried your code and it didn't work at first, but i just erased the 'c' and wrote again and all of the sudden it worked perfectly
    Actually, I re-wrote it too, and now it works :S However the previous and the current implementations were identical :S What could it be? :S

  8. #8
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    i have no idea to be honest, but maybe it is a print error, it often happens
    Click on REP and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  2. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  3. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 10:20 PM
  4. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-08-2008, 12:33 AM
  5. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 09:56 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
  •