Click here to Skip to main content
16,021,449 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i got the output in command prompt.but when i try to get the answer in text file..my coding build successfully...but shows empty file

What I have tried:

Java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class ll {

public static void main(String[] args) {
    FileReader fr = null;
    BufferedReader br =null;

    String [] stringArray;
    int counLine = 0;
    int arrayLength ;
    String s="";
    String stringLine="";
    try{
     //   fr = new FileReader("F:\\New folder (2)\\N.txt");
        String wrt="F:\\New folder (2)\\N.txt";
String line=null;
 fr=new FileReader(wrt);
FileWriter fw=new FileWriter(wrt);
BufferedWriter bw=new BufferedWriter(fw);
 
        br = new BufferedReader(fr);
        while((s = br.readLine()) != null){
            stringLine = stringLine + line;
            stringLine = stringLine + " ";
            counLine ++;  
        }System.out.println(stringLine);
 while((line=br.readLine())!=null)
       {
          System.out.print(line);
        }
        br.close();
        stringArray = stringLine.split(" ");
        arrayLength = stringArray.length;
     for (int i = 0; i < arrayLength; i++) {
            int c = 1 ;
            for (int j = i+1; j < arrayLength; j++) {
                if(stringArray[i].equalsIgnoreCase(stringArray[j])){
                   c++;
                   for (int j2 = j; j2 < arrayLength; j2++) {
                 }
     }
                int k;
              for(k=2;k==stringArray[i].length();i++)
           
             // if(stringArray[i].length()==1)
                {     bw.write(stringArray[i]);
              //    System.out.println(stringArray[i]);  
              }
            }}

//bw.newLine();
//bw.write("hello");
bw.close();
     fr.close();
        br.close();
    }catch (Exception e) {
        e.printStackTrace();
    }}}
Posted
Updated 9-Sep-16 22:00pm
v2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Your program is very confusing and some of the code is redundant. Your first loop reads each line of the file into the variable named s, but does nothing with that data. It repeatedly tries to concatenate stringLine and line, but neither of them contain any text. You then start a second loop trying to read the file, but since it is already at EOF (from the first loop) there is nothing to read. Following that you attempt to write something back to the same file, thus destroying any of the original data. If you must do reading and writing in the same program then use two different files so you do not corrupt your original test data. Also, try reading through your program line by line to check the logic.
 
Share this answer
 
Comments
Member 12200805 10-Sep-16 8:23am    
below is my first coding which gave me a correct output in the comment prompt..so wat should i do to get this output in the file

mport java.io.BufferedReader;
import java.io.FileReader;

public class gg {

public static void main(String[] args) {
FileReader fr = null;
BufferedReader br =null;

String [] stringArray;
int counLine = 0;
int arrayLength ;
String s="";
String stringLine="";
try{
fr = new FileReader("D:\\surya\\New folder\\ss\\jj.txt");
br = new BufferedReader(fr);
while((s = br.readLine()) != null){
stringLine = stringLine + s;
stringLine = stringLine + " "
counLine ++;
}System.out.println(stringLine);

stringArray = stringLine.split(" ");
arrayLength = stringArray.length;
for (int i = 0; i < arrayLength; i++) {
int c = 1 ;
for (int j = i+1; j < arrayLength; j++) {
if(stringArray[i].equalsIgnoreCase(stringArray[j])){
c++;
for (int j2 = j; j2 < arrayLength; j2++) {
}
}
int k;
for(k=2;k==stringArray[i].length();i++)

// if(stringArray[i].length()==1)
{
System.out.println(stringArray[i]);
}}} fr.close();
br.close();
}catch (Exception e) {
e.printStackTrace();
}}}
[no name] 10-Sep-16 9:08am    
Instead of using System.out.println, just write the same data to your file. See Lesson: Basic I/O (The Java&trade; Tutorials &gt; Essential Classes)[^] for full details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900