java - I need the length of an array element, and then print the last character -
so, today, given school project, , need help.
i need find first , last letter of each element in array, , print it. of java people out there, please tell me function, method, or bit of code need in order this?
one of issues comes when compile , run says charat(a);, tells me cannot take character there, whatever reason.
so, if me work out, i'll grateful, due tonight, , cannot figure out.
thanks much.
/** * write description of class pokemon here. * * @author (your name) * @version (a version number or date) */ import java.util.scanner; public class pokemon { public static void main (string [ ] args) { scanner scan = new scanner(system.in); string array [ ] = new string [ 10 ]; array [ 0 ] = "charizard"; array [ 1 ] = "pikachu"; array [ 2 ] = "jigglypuff"; array [ 3 ] = "timburr"; array [ 4 ] = "conkeldurr"; array [ 5 ] = "gurdurr"; system.out.println("------------------------========================------------------------"); system.out.println(" "); system.out.println(" .\"-,.__"); system.out.println(" `. `. ,"); system.out.println(" .--' .._,'\"-' `."); system.out.println(" . .' `'"); system.out.println(" `. / ,'"); system.out.println(" ` '--. ,-\"'"); system.out.println(" `\"` | \\"); system.out.println(" -. \\, |"); system.out.println(" `--y.' ___."); system.out.println(" \\ l._, \\"); system.out.println(" _., `. < <\\ _"); system.out.println(" ,' ' `, `. | \\ ( `"); system.out.println(" ../, `. ` | .\\`. \\ \\_"); system.out.println(" ,' ,.. . _.,' ||\\l ) '\"."); system.out.println(" , ,' \\ ,'.-.`-._,' | . _._`."); system.out.println(" ,' / \\ \\ `' ' `--/ | \\ / / ..\\"); system.out.println(" .' / \\ . |\\__ - _ ,'` ` / / `.`."); system.out.println(" | ' .. `-...-\" | `-' / / . `."); system.out.println(" | / |l__ | | / / `. `."); system.out.println(" , / . . | | / / ` `"); system.out.println(" / / ,. ,`._ `-_ | | _ ,-' / ` \\"); system.out.println(" / . \"`_/. `-_ \\_,. ,' +-' `-' _, ..,-. \\`."); system.out.println(" ' .-f ,' ` '. \\__.---' _ .' ' \\ \\"); system.out.println("' / `.' l .' / \\.. ,_|/ `. ,'` l`"); system.out.println("|' _.-\"\"` `. \\ _,' ` \\ `.___`.'\"`-. , | | | \\"); system.out.println("|| ,' `. `. ' _,...._ ` | `/ ' | ' .|"); system.out.println("|| ,' `. ;.,.---' ,' `. `.. `-' .-' /_ .' ;_ ||"); system.out.println("|| ' v / / ` | ` ,' ,' '. ! `. ||"); system.out.println("||/ _,-------7 ' . | `-' l / `||"); system.out.println(" | ,' .- ,' || | .-. `. .' ||"); system.out.println(" `' ,' `\".' | | `. '. -.' `'"); system.out.println(" / ,' | |,' \\-.._,.'/'"); system.out.println(" . / . . \\ .''"); system.out.println(" .`. | `. / :_,'.'"); system.out.println(" \\ `...\\ _ ,'-. .' /_.-'"); system.out.println(" `-.__ `, `' . _.>----''. _ __ /"); system.out.println(" .' /\"' | \"' '_"); system.out.println(" /_|.-'\\ ,\\\". '.'`__'-( \\"); system.out.println(" / ,\\\"'\\\"\\,' `/ `-.|\""); system.out.println(" "); system.out.println("------------------------========charizard========------------------------"); system.out.println(" "); system.out.println(" "); system.out.println("items in array: "); system.out.println(" "); system.out.println("0 - " + array [ 0 ]); system.out.println("1 - " + array [ 1 ]); system.out.println("2 - " + array [ 2 ]); system.out.println("3 - " + array [ 3 ]); system.out.println("4 - " + array [ 4 ]); system.out.println("5 - " + array [ 5 ]); system.out.println(" "); system.out.println(" "); int = (array [ 0 ]).length(); int b = (array [ 1 ]).length(); int c = (array [ 2 ]).length(); int d = (array [ 3 ]).length(); int e = (array [ 4 ]).length(); int f = (array [ 5 ]).length(); char g = (array [ 0 ]).charat(0); char h = (array [ 1 ]).charat(0); char = (array [ 2 ]).charat(0); char j = (array [ 3 ]).charat(0); char k = (array [ 4 ]).charat(0); char l = (array [ 5 ]).charat(0); char m = (array [ 0 ]).charat(a); char n = (array [ 1 ]).charat(b); char o = (array [ 2 ]).charat(c); char p = (array [ 3 ]).charat(d); char q = (array [ 4 ]).charat(e); char r = (array [ 5 ]).charat(f); system.out.println(array [ 0 ] + " - first letter of element: " + g + " second letter of element: " + m); system.out.println(array [ 1 ] + " - first letter of element: " + h + " second letter of element: " + n); system.out.println(array [ 2 ] + " - first letter of element: " + + " second letter of element: " + o); system.out.println(array [ 3 ] + " - first letter of element: " + j + " second letter of element: " + p); system.out.println(array [ 4 ] + " - first letter of element: " + k + " second letter of element: " + q); system.out.println(array [ 5 ] + " - first letter of element: " + l + " second letter of element: " + r); }
}
you have use,
char m = (array [ 0 ]).charat(a-1); char n = (array [ 1 ]).charat(b-1); char o = (array [ 2 ]).charat(c-1); char p = (array [ 3 ]).charat(d-1); char q = (array [ 4 ]).charat(e-1); char r = (array [ 5 ]).charat(f-1);
becuse indexing starts 0 , last letter's index (length-1)
but better use loop this
for(int v=0;v<6;v++){ system.out.println(array [ v ] + " - first letter of element: " + array[v].charat(0) + " second letter of element: " + array[v].charat(array[v].length()-1)); }
Comments
Post a Comment