java - Refactoring lots of if statements -
so have project create random-name generator, , have prefixes , suffixes beingness selected by: a) amount of letters in person's first name, , b) first , lastly letter of name.
the code functions expected, i'd refine code , remove hundreds of lines have thousands of if statements.
import java.util.*; public class ranname { public static void main(string[] args) { string input, firstname, lastname; string firstpre, firstsuff, lastpre, lastsuff, lsuffmean, lpremean, flastlet, ffirstlet, llastlet, lfirstlet; scanner sc = new scanner(system.in); system.out.println("welcome lord of rings elf name generator!"); system.out.println("------------------------------------------------"); system.out.println(""); system.out.print("first name: "); firstname = sc.nextline(); while (true) { system.out.println("------------------------------------------------"); system.out.println("you inserted " + firstname); system.out.println("are sure?"); system.out.print("y/n: "); input = sc.nextline(); system.out.println("------------------------------------------------"); if (input.equalsignorecase("y")) { break; } else if (input.equalsignorecase("n")) { system.out.print("first name: "); firstname = sc.nextline(); } else { system.out.println("oh well, tried. here's go @ it."); } } system.out.print("last name: "); lastname = sc.nextline(); while (true) { system.out.println("------------------------------------------------"); system.out.println("you inserted " + lastname); system.out.println("are sure?"); system.out.print("y/n: "); input = sc.nextline(); system.out.println("------------------------------------------------"); if (input.equalsignorecase("y")) { break; } else if (input.equalsignorecase("n")) { system.out.print("last name: "); lastname = sc.nextline(); } else { system.out.println("oh well, tried. here's go @ it."); } } system.out.print("your elf name: "); firstpre = preget(firstname); firstsuff = suffget(firstname); lastpre = housepreget(lastname); lastsuff = housesuffget(lastname); lpremean = premean(lastpre); lsuffmean = suffmean(lastsuff); flastlet = string.valueof(firstpre.charat(firstpre.length()-1)); ffirstlet = string.valueof(firstsuff.charat(0)); llastlet = string.valueof(lastpre.charat(lastpre.length()-1)); lfirstlet = string.valueof(lastsuff.charat(0)); if (ffirstlet.equals(flastlet)) { firstsuff = (firstsuff.substring(1)); } if (lfirstlet.equals(llastlet)) { lastsuff = (lastsuff.substring(1)); } system.out.println(firstpre + firstsuff + " " + lastpre + lastsuff); system.out.println(""); system.out.println("------------------------------------------------"); system.out.println("the house name (lastname) translates to: " + lpremean + " " + lsuffmean); system.out.println("------------------------------------------------"); } public static string preget(string fn) { string[] namepre; string fnn, fl; fnn = fn.trim(); int fncount = fnn.length(); fl = string.valueof(fnn.charat(0)); namepre = new string[53]; namepre[0] = "placeholder"; namepre[1] = "ael"; namepre[2] = "aer"; namepre[3] = "bael"; namepre[4] = "bes"; namepre[5] = "cael"; namepre[6] = "cor"; namepre[7] = "dae"; namepre[8] = "dre"; namepre[9] = "eil"; namepre[10] = "ev"; namepre[11] = "fir"; namepre[12] = "fis"; namepre[13] = "gael"; namepre[14] = "gil"; namepre[15] = "ha"; namepre[16] = "hu"; namepre[17] = "ia"; namepre[18] = "il"; namepre[19] = "ja"; namepre[20] = "jar"; namepre[21] = "kan"; namepre[22] = "kor"; namepre[23] = "la"; namepre[24] = "lue"; namepre[25] = "mai"; namepre[26] = "mara"; namepre[27] = "na"; namepre[28] = "nim"; namepre[29] = "ol"; namepre[30] = "onn"; namepre[31] = "py"; namepre[32] = "pael"; namepre[33] = "qu"; namepre[34] = "qi"; namepre[35] = "rum"; namepre[36] = "rua"; namepre[37] = "sae"; namepre[38] = "sha"; namepre[39] = "tahl"; namepre[40] = "thro"; namepre[41] = "ul"; namepre[42] = "uon"; namepre[43] = "ver"; namepre[44] = "vil"; namepre[45] = "wuo"; namepre[46] = "waal"; namepre[47] = "xae"; namepre[48] = "xen"; namepre[49] = "ya"; namepre[50] = "yae"; namepre[51] = "za"; namepre[52] = "zy"; if (fncount % 2 == 0) { if (fl.equalsignorecase("a")) { homecoming namepre[1]; } else if (fl.equalsignorecase("b")) { homecoming namepre[3]; } else if (fl.equalsignorecase("c")) { homecoming namepre[5]; } else if (fl.equalsignorecase("d")) { homecoming namepre[7]; } else if (fl.equalsignorecase("e")) { homecoming namepre[9]; } else if (fl.equalsignorecase("f")) { homecoming namepre[11]; } else if (fl.equalsignorecase("g")) { homecoming namepre[13]; } else if (fl.equalsignorecase("h")) { homecoming namepre[15]; } else if (fl.equalsignorecase("i")) { homecoming namepre[17]; } else if (fl.equalsignorecase("j")) { homecoming namepre[19]; } else if (fl.equalsignorecase("k")) { homecoming namepre[21]; } else if (fl.equalsignorecase("l")) { homecoming namepre[23]; } else if (fl.equalsignorecase("m")) { homecoming namepre[25]; } else if (fl.equalsignorecase("n")) { homecoming namepre[27]; } else if (fl.equalsignorecase("o")) { homecoming namepre[29]; } else if (fl.equalsignorecase("p")) { homecoming namepre[31]; } else if (fl.equalsignorecase("q")) { homecoming namepre[33]; } else if (fl.equalsignorecase("r")) { homecoming namepre[35]; } else if (fl.equalsignorecase("s")) { homecoming namepre[37]; } else if (fl.equalsignorecase("t")) { homecoming namepre[39]; } else if (fl.equalsignorecase("u")) { homecoming namepre[41]; } else if (fl.equalsignorecase("v")) { homecoming namepre[43]; } else if (fl.equalsignorecase("w")) { homecoming namepre[45]; } else if (fl.equalsignorecase("x")) { homecoming namepre[47]; } else if (fl.equalsignorecase("y")) { homecoming namepre[49]; } else if (fl.equalsignorecase("z")) { homecoming namepre[51]; } } else { if (fl.equalsignorecase("a")) { homecoming namepre[2]; } else if (fl.equalsignorecase("b")) { homecoming namepre[4]; } else if (fl.equalsignorecase("c")) { homecoming namepre[6]; } else if (fl.equalsignorecase("d")) { homecoming namepre[8]; } else if (fl.equalsignorecase("e")) { homecoming namepre[10]; } else if (fl.equalsignorecase("f")) { homecoming namepre[12]; } else if (fl.equalsignorecase("g")) { homecoming namepre[14]; } else if (fl.equalsignorecase("h")) { homecoming namepre[16]; } else if (fl.equalsignorecase("i")) { homecoming namepre[18]; } else if (fl.equalsignorecase("j")) { homecoming namepre[20]; } else if (fl.equalsignorecase("k")) { homecoming namepre[22]; } else if (fl.equalsignorecase("l")) { homecoming namepre[24]; } else if (fl.equalsignorecase("m")) { homecoming namepre[26]; } else if (fl.equalsignorecase("n")) { homecoming namepre[28]; } else if (fl.equalsignorecase("o")) { homecoming namepre[30]; } else if (fl.equalsignorecase("p")) { homecoming namepre[32]; } else if (fl.equalsignorecase("q")) { homecoming namepre[34]; } else if (fl.equalsignorecase("r")) { homecoming namepre[36]; } else if (fl.equalsignorecase("s")) { homecoming namepre[38]; } else if (fl.equalsignorecase("t")) { homecoming namepre[40]; } else if (fl.equalsignorecase("u")) { homecoming namepre[42]; } else if (fl.equalsignorecase("v")) { homecoming namepre[44]; } else if (fl.equalsignorecase("w")) { homecoming namepre[46]; } else if (fl.equalsignorecase("x")) { homecoming namepre[48]; } else if (fl.equalsignorecase("y")) { homecoming namepre[50]; } else if (fl.equalsignorecase("z")) { homecoming namepre[52]; } } homecoming ""; } public static string suffget(string fn) { string[] namesuff; string fnn, fl; fnn = fn.trim(); int fncount = fnn.length(); fl = string.valueof(fnn.charat(fnn.length()-1)); namesuff = new string[53]; namesuff[0] = "placeholder"; namesuff[1] = "ae"; namesuff[2] = "aith"; namesuff[3] = "brar"; namesuff[4] = "bael"; namesuff[5] = "cael"; namesuff[6] = "con"; namesuff[7] = "drimme"; namesuff[8] = "dul"; namesuff[9] = "emar"; namesuff[10] = "evar"; namesuff[11] = "fel"; namesuff[12] = "faen"; namesuff[13] = "gael"; namesuff[14] = "gin"; namesuff[15] = "hal"; namesuff[16] = "har"; namesuff[17] = "ii"; namesuff[18] = "im"; namesuff[19] = "jin"; namesuff[20] = "jaal"; namesuff[21] = "ki"; namesuff[22] = "kas"; namesuff[23] = "lian"; namesuff[24] = "lihn"; namesuff[25] = "mah"; namesuff[26] = "'mek"; namesuff[27] = "nes"; namesuff[28] = "'nil"; namesuff[29] = "onna"; namesuff[30] = "oth"; namesuff[31] = "pae"; namesuff[32] = "pek"; namesuff[33] = "'que"; namesuff[34] = "quis"; namesuff[35] = "ruil"; namesuff[36] = "reth"; namesuff[37] = "san"; namesuff[38] = "sel"; namesuff[39] = "thal"; namesuff[40] = "thus"; namesuff[41] = "ual"; namesuff[42] = "uath"; namesuff[43] = "vain"; namesuff[44] = "vin"; namesuff[45] = "wyn"; namesuff[46] = "waal"; namesuff[47] = "'xe"; namesuff[48] = "'xol"; namesuff[49] = "yth"; namesuff[50] = "yl"; namesuff[51] = "zair"; namesuff[52] = "zara"; if (fncount % 2 != 0) { if (fl.equalsignorecase("a")) { homecoming namesuff[1]; } else if (fl.equalsignorecase("b")) { homecoming namesuff[3]; } else if (fl.equalsignorecase("c")) { homecoming namesuff[5]; } else if (fl.equalsignorecase("d")) { homecoming namesuff[7]; } else if (fl.equalsignorecase("e")) { homecoming namesuff[9]; } else if (fl.equalsignorecase("f")) { homecoming namesuff[11]; } else if (fl.equalsignorecase("g")) { homecoming namesuff[13]; } else if (fl.equalsignorecase("h")) { homecoming namesuff[15]; } else if (fl.equalsignorecase("i")) { homecoming namesuff[17]; } else if (fl.equalsignorecase("j")) { homecoming namesuff[19]; } else if (fl.equalsignorecase("k")) { homecoming namesuff[21]; } else if (fl.equalsignorecase("l")) { homecoming namesuff[23]; } else if (fl.equalsignorecase("m")) { homecoming namesuff[25]; } else if (fl.equalsignorecase("n")) { homecoming namesuff[27]; } else if (fl.equalsignorecase("o")) { homecoming namesuff[29]; } else if (fl.equalsignorecase("p")) { homecoming namesuff[31]; } else if (fl.equalsignorecase("q")) { homecoming namesuff[33]; } else if (fl.equalsignorecase("r")) { homecoming namesuff[35]; } else if (fl.equalsignorecase("s")) { homecoming namesuff[37]; } else if (fl.equalsignorecase("t")) { homecoming namesuff[39]; } else if (fl.equalsignorecase("u")) { homecoming namesuff[41]; } else if (fl.equalsignorecase("v")) { homecoming namesuff[43]; } else if (fl.equalsignorecase("w")) { homecoming namesuff[45]; } else if (fl.equalsignorecase("x")) { homecoming namesuff[47]; } else if (fl.equalsignorecase("y")) { homecoming namesuff[49]; } else if (fl.equalsignorecase("z")) { homecoming namesuff[51]; } } else { if (fl.equalsignorecase("a")) { homecoming namesuff[2]; } else if (fl.equalsignorecase("b")) { homecoming namesuff[4]; } else if (fl.equalsignorecase("c")) { homecoming namesuff[6]; } else if (fl.equalsignorecase("d")) { homecoming namesuff[8]; } else if (fl.equalsignorecase("e")) { homecoming namesuff[10]; } else if (fl.equalsignorecase("f")) { homecoming namesuff[12]; } else if (fl.equalsignorecase("g")) { homecoming namesuff[14]; } else if (fl.equalsignorecase("h")) { homecoming namesuff[16]; } else if (fl.equalsignorecase("i")) { homecoming namesuff[18]; } else if (fl.equalsignorecase("j")) { homecoming namesuff[20]; } else if (fl.equalsignorecase("k")) { homecoming namesuff[22]; } else if (fl.equalsignorecase("l")) { homecoming namesuff[24]; } else if (fl.equalsignorecase("m")) { homecoming namesuff[26]; } else if (fl.equalsignorecase("n")) { homecoming namesuff[28]; } else if (fl.equalsignorecase("o")) { homecoming namesuff[30]; } else if (fl.equalsignorecase("p")) { homecoming namesuff[32]; } else if (fl.equalsignorecase("q")) { homecoming namesuff[34]; } else if (fl.equalsignorecase("r")) { homecoming namesuff[36]; } else if (fl.equalsignorecase("s")) { homecoming namesuff[38]; } else if (fl.equalsignorecase("t")) { homecoming namesuff[40]; } else if (fl.equalsignorecase("u")) { homecoming namesuff[42]; } else if (fl.equalsignorecase("v")) { homecoming namesuff[44]; } else if (fl.equalsignorecase("w")) { homecoming namesuff[46]; } else if (fl.equalsignorecase("x")) { homecoming namesuff[48]; } else if (fl.equalsignorecase("y")) { homecoming namesuff[50]; } else if (fl.equalsignorecase("z")) { homecoming namesuff[52]; } homecoming ""; } homecoming ""; }
string fnn, fl; fnn = fn.trim(); int fncount = fnn.length(); fl = string.valueof(fnn.charat(fnn.length()-1));
can replaced with
fn = fn.trim(); char fl = fn.charat(fn.length()-1);
then can utilize ascii code of letter determine array index should
fn = fn.trim(); char fl = fn.charat(fn.length()-1); string[] suffixodd = {"aith", "bael", "con", "dul", "evar", "faen", "gin", "har", "im", "jaal", "kas", "lihn", "'mek", "'nil", "oth", "pek", "quis", "reth", "sel", "thus", "uath", "vin", "waal", "'xol", "yl", "zara"}; string[] suffixeven = {"ae", "brar", "cael", "drimme", "emar", "fel", "gael", "hal", "ii", "jin", "ki", "lian", "mah", "nes", "onna", "pae", "'que", "ruil", "san", "thal", "ual", "vain", "wyn", "'xe", "yth", "zair"}; int suffixindex = character.touppercase(fl) - 'a'; if (fn.length() % 2 != 0) { if(suffixindex >= suffixodd.length) homecoming ""; homecoming suffixodd[character.touppercase(fl) - 'a']; } else { if(suffixindex >= suffixeven.length) homecoming ""; homecoming suffixeven[character.touppercase(fl) - 'a']; }
in case suffix , odd both 26 long should set length checks in each of respective code blocks.
ps: highly encourage type out more descriptive variable names fn
, fl
, lastpre
ect... bad names, have no thought are. future give thanks you.
java if-statement refactoring
No comments:
Post a Comment