Note that there are some explanatory texts on larger screens.

plurals
  1. POInterview Coding Java Sorting
    primarykey
    data
    text
    <p>Write a java program to read input from a file, and then sort the characters within each word. Once you have done that, sort all the resulting words in ascending order and finally followed by the sum of numeric values in the file.</p> <ul> <li>Remove the special characters and stop words while processing the data</li> <li>Measure the time taken to execute the code</li> </ul> <p>Lets Say the content of file is: Sachin Tendulkar scored 18111 ODI runs and 14692 Test runs.</p> <p>Output:achins adeklnrtu adn cdeors dio estt nrsu nrsu 32803</p> <p>Time Taken: 3 milliseconds</p> <p>My Code takes 15milliseconds to execute.....</p> <p>please suggest me any fast way to solve this problem...........</p> <p>Code:</p> <pre><code>import java.io.BufferedReader; import java.io.FileReader; import java.util.*; public class Sorting { public static void main(String[] ags)throws Exception { long st=System.currentTimeMillis(); int v=0; List ls=new ArrayList(); //To read data from file BufferedReader in=new BufferedReader( new FileReader("D:\\Bhive\\File.txt")); String read=in.readLine().toLowerCase(); //Spliting the string based on spaces String[] sp=read.replaceAll("\\.","").split(" "); for(int i=0;i&lt;sp.length;i++) { //Check for the array if it matches number if(sp[i].matches("(\\d+)")) //Adding the numbers v+=Integer.parseInt(sp[i]); else { //sorting the characters char[] c=sp[i].toCharArray(); Arrays.sort(c); String r=new String(c); //Adding the resulting word into list ls.add(r); } } //Sorting the resulting words in ascending order Collections.sort(ls); //Appending the number in the end of the list ls.add(v); //Displaying the string using Iteartor Iterator it=ls.iterator(); while(it.hasNext()) System.out.print(it.next()+" "); long time=System.currentTimeMillis()-st; System.out.println("\n Time Taken:"+time); } } </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.
 

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