Write a java program to read input from a file and sort the words and then the whole file again.
This code reads only the 1st line of the input text file and produces the o/p for that line only, the lines that are present in the next sentences(\n) are not executed.
I tried to solve this by carriage return and new line, but it was still in vain.
Please suggest me a proper solution to solve this problem..
code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Sorting
{
public static void main(String[] args)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:/hari1.txt"));
while((line = in.readLine().toLowerCase()) != null)
{
String[] sp=read.replaceAll("[-+.^:,!@#$%&*()~?]"," ").split(" ");
}
for(int i=0;i<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);
}
}