Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting strings by the numbers of characters
    primarykey
    data
    text
    <p>I have a problem. I want to sort Strings from txt file by the numbers of characters in String For example Input: aaa a aa Output: a aa aaa I created Collection that contain strings by alphabet &amp; created collection that contain number of String characters, but i can't understand </p> <p>How I can sort by the numbers of characters in String</p> <pre><code>public class SortingFile { public static void main(String[] args) throws IOException { try{ File inputFile=new File("c:/a.txt"); Reader r=new FileReader(inputFile); BufferedReader br=new BufferedReader(r); List &lt;String&gt; list = new LinkedList&lt;String&gt;(); List &lt;Integer&gt; listLength= new LinkedList&lt;Integer&gt;(); String line=br.readLine(); list.add(line); while (line!=null){ line=br.readLine(); list.add(line); } int arrsize=(list.size())-1; System.out.println("line array size= "+arrsize); list.remove(arrsize); //delete last element (he is null ) System.out.println("Before sorting by alphabet: "+list); Collections.sort(list);//sorting by alphabet System.out.println("After sorting by alphabet: " +list); for (int i=0;i!=list.size();i++){ //add string lenght to collection String a=list.get(i); int aa=a.length(); listLength.add(aa); } System.out.println("lineLength:"+listLength); Collections.sort(listLength); // sotring by asc System.out.println("lineLength2:"+listLength); br.close(); }catch (FileNotFoundException e1){ System.out.println("File ''a.txt'' Not Found :("); e1.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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