Issue
I have the following code
ArrayList<String> proposta = new ArrayList<String>();
String propostaString = "";
proposta.add("213141441414");
proposta.add("515151551");
proposta.add("2626262662");
proposta.add("26262627373");
proposta.add("7373632525");
proposta.add("1515252");
proposta.add("262636474");
proposta.add("11414142222");
String entrada = proposta.toString();
String lastDigitsClient = "2222";
String lastFour = "2222";
boolean Equal = false;
if(lastDigitsClient.contains(lastFour)) {
Equal=true;
System.out.println("ULTIMOS DIGITOS : TRUE = " +Equal);
System.out.println("Lista de Propostas: " +entrada);
}
I want to print the proposta 11414142222 using the value of lastDigitsClient, how could I do it?
Solution
For a simple approach, I would expect something more like:
ArrayList<String> proposta = new ArrayList<String>();
proposta.add("213141441414");
proposta.add("515151551");
proposta.add("2626262662");
proposta.add("26262627373");
proposta.add("7373632525");
proposta.add("1515252");
proposta.add("262636474");
proposta.add("11414142222");
String match = "";
boolean matchFound = false;
String lastFour = "2222";
for(String s : proposta) {
if(s.endsWith(lastFour)) {
match = s;
matchFound = true;
break;
}
}
if (matchFound) {
System.out.println("match = " + match);
}
else {
System.out.println("No match found.");
}
Answered By - Idle_Mind
Answer Checked By - Marilyn (JavaFixing Volunteer)