Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
    }
}
share|improve this question
3  
Walk through your code. How many times do you call readLine? – Devon_C_Miller Oct 28 '12 at 1:20
i did in this way, but i got a error, can you please rectify the mistake in this – Hari Priya R Oct 28 '12 at 9:50
while((line = in.readLine()) != null) { String[] sp=read.replaceAll("[-+.^:,!@#$%&*()~?]","").split(" "); } – Hari Priya R Oct 28 '12 at 9:51
can you please alter this coding in the same program of mine... i am getting some errors in my looping of readline() – Hari Priya R Oct 28 '12 at 10:08
i have edited the program in my post, but still there r errors in the code for split and the following codes.. – Hari Priya R Oct 28 '12 at 10:42
show 3 more comments

closed as too localized by Brian Roach, Bohemian, SimpleCoder, DNA, user714965 Oct 28 '12 at 18:13

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

You only have one call to readLine(), therefore you only read the first line!

To read the whole file, you have to call readLine() in a loop until there are no more lines left. You can find many examples of this online, e.g.:

String line;
BufferedReader in = new BufferedReader(new FileReader("input.txt"));
while((line = in.readLine()) != null) {
    // process the line
}
share|improve this answer
can you please say me how to code readline() in a loop, based on what ? – Hari Priya R Oct 28 '12 at 2:29
thank you very much .. – Hari Priya R Oct 28 '12 at 9:08
if i place the readline() in a loop, should i make the split based on \n also ? bcz it shows an error in dis line of the code – Hari Priya R Oct 28 '12 at 9:44
'while((line = in.readLine()) != null) { String[] sp=read.replaceAll("[-+.^:,!@#$%&*()~?]"," ").split(" "); }' – Hari Priya R Oct 28 '12 at 9:53
can you please alter this coding in the same program of mine... i am getting some errors in my looping of readline() – Hari Priya R Oct 28 '12 at 10:04
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.