Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch ArrayList for certain character in string
    primarykey
    data
    text
    <p><em>What is the correct syntax for searching an <code>ArrayList</code> of strings for a single character? I want to check each string in the array for a single character. Ultimately I want to perform multiple search and replaces on all strings in an array based on the presence of a single character in the string. I have reviewed java-examples.com and java docs as well as several methods of searching ArrayLists. None of them do quite what I need. P.S. Any pointers on using some sort of file library to perform multiple search and replaces would be great.</em></p> <p><strong>--- Edit ---</strong></p> <p>As per MightyPork's recommendations <code>arraylist</code> revised to use simple <code>string</code> type. This also made it compatible with hoosssein's solution which is included.</p> <hr> <pre><code> public void ArrayInput() { String FileName; // set file variable FileName = fileName.getText(); // get file name ArrayList&lt;String&gt; fileContents = new ArrayList&lt;String&gt;(); // create arraylist try { BufferedReader reader = new BufferedReader(new FileReader(FileName)); // create reader String line = null; while ((line = reader.readLine()) != null) { if(line.length() &gt; 0){ // don't include blank lines line = line.trim(); // remove whitespaces fileContents.add(line); // add to array } } for (String row : fileContents) { System.out.println(row); // print array to cmd } String oldstr; String newstr; oldstr = "}"; newstr = "!!!!!"; for(int i=0;i&lt;fileContents.size();i++){ if(fileContents.contains(oldstr)){ fileContents.set(i, fileContents.get(i).replace(oldstr, newstr)); } } for (String row : fileContents) { System.out.println(row); // print array to cmd } // close file } catch (IOException ex) { // E.H. for try JOptionPane.showMessageDialog(null, "File not found. Check name and directory."); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload