Return strings in array that begin with the initial passed

Return strings in array that begin with the initial passed



I have a .txt file which has data for states as given below:


AL,Alab,4860
AK,Alas,7415
AZ,Ariz,6908
AR,Arka,2988



I have made a function which counts how many states there are that start with the initial passed as such:


public int CInitial(char initial)
int total = 0;
for(int i = 0; i < states.length; i++) //states is an array which includes all states present in the .txt file
String testString = states[i].getName(); // getName gets the name of the states present in the .txt file
char stringToCharArray = testString.toCharArray();
for (char output : stringToCharArray)
if(initial == output)
total++;





return total;



This would return the number 4 if "A" is passed and 0 if any other initial is passed as there are 4 states that begin with the letter "A".



Now how can I create a new function that passes a character and returns the name of all the states that begin with that character? For Instance this is the initial return type needed for this, however I'm having troubles starting this. Is the process identical to the countStatesCountByInitial function I created?


public State CByInitial(char initial)
return new State ; //to be completed






Include the full definition of your State class.

– Tim Biegeleisen
Sep 2 '18 at 5:13


State






there is no need to add the eclipse tag if your question is not specifically related to a problem with eclipse itself.

– Patrick Parker
Sep 2 '18 at 5:24






for (char output : stringToCharArray) { why are you looping over the whole string if you only want to check the first char? hint: use String#startswith

– Patrick Parker
Sep 2 '18 at 5:27


for (char output : stringToCharArray) {






"returns the name of all the states that begin with that character" - then your method should look like this: public String getSomething(char initial)

– Patrick Parker
Sep 2 '18 at 5:32


public String getSomething(char initial)






DRY(Don't Repeat Yourself), the second method returning State is enough.

– user2575725
Sep 2 '18 at 5:48


State




2 Answers
2



Yes, it will be very similar to the countStatesCountByInitial. The main difference is each time you find a match, you want to add the state into the array. Since we don't know the size of the array beforehand, we may want to use a List instead.


countStatesCountByInitial


List


public State getStatesCountByInitial(char initial)
ArrayList<State> found = new ArrayList<>();

// this is the same as before
for(int i = 0; i < states.length; i++)
String testString = states[i].getName();
char stringToCharArray = testString.toCharArray();
for (char output : stringToCharArray)
if(initial == output)
// except here when you find a match, you add it into the list
found.add(states[i]);




// return as array
return found.toArray(new State[found.size()]);



As suggested by Patrick, we can avoid using List by using countStatesCountByInitial to initialize the size of the states.


List


countStatesCountByInitial


public State getStatesCountByInitial(char initial)
int matchSize = countStatesCountByInitial(initial);
States found = new States[matchSize];
int foundIndex = 0;

// this is the same as before
for(int i = 0; i < states.length; i++)
String testString = states[i].getName();
char stringToCharArray = testString.toCharArray();
for (char output : stringToCharArray)
if(initial == output)
// except here when you find a match, you add it into the array
found[foundIndex] = states[i];
foundIndex++;




// return the array
return found;






or rather than introducing the concept of lists when learning arrays, you could use countStatesCountByInitial to get the size of the array.

– Patrick Parker
Sep 2 '18 at 5:34


countStatesCountByInitial






@Patrick, that's a good idea. Let me edit my answer.

– Magdrop
Sep 2 '18 at 5:45






for (char output : stringToCharArray) { why are you looping over the whole string if you only want to check the first char? hint: use String#startswith

– Patrick Parker
Sep 2 '18 at 10:31


for (char output : stringToCharArray) {






@Magdrop your original answer actually worked instead of this one

– Utsav Bhattarai
Sep 5 '18 at 12:22






@Magdrop also, instead of return I tried printing the output but it has a gibberish output as [LstateInformation.State;@5f4da5c3 not quite sure what's happening?

– Utsav Bhattarai
Sep 5 '18 at 12:24



You can done both operations simply by one method.


public static ArrayList<State> getStatesCountByInitial(char initial)
ArrayList selectedStates = new ArrayList<State>();
for(int i = 0; i < states.length; i++)
if(states.charAt(0) == initial)
selectedStates.add(states[i]);


return selectedStates;



This method will return a arraylist.
If you want to get the count, call this method and get the size of the array.


ArrayList<State> statesNew = getStatesCountByInitial('A');
int count = statesNew.size();



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế