PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] Why program did not show answer in out.txt?

 August 14, 2022     java, output, stringbuffer, stringbuilder, text     No comments   

Issue

The code should do a reverse and output the result to out.txt, but this does not happen, can you explain my mistake in the code. Thanks in advance

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        FileReader input = new FileReader("in.txt");
        FileWriter output = new FileWriter("out.txt");
        BufferedReader sb = new BufferedReader(input);
        String data;

        while ((data = sb.readLine()) != null) {
            String[] words = data.split("                                  ");
            for (String a : words) {
                StringBuilder builder = new StringBuilder(a);
                builder.reverse();

                while ((sb.read()) != -1) {
                    output.write(String.valueOf(builder.reverse()));
                }
            }
        }
    }
}

Solution

You are trying to reverse the string twice because of that the string is getting back to the original string. Also, there is an unnecessary (as per my understanding) while loop inside the for loop (I have removed that in my answer).

Try the below code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        FileReader input = new FileReader("in.txt");
        FileWriter output = new FileWriter("out.txt");
        BufferedReader sb = new BufferedReader(input);
        String data;

        while ((data = sb.readLine()) != null) {
            String[] words = data.split("                                  ");
            // above statement can be replaced with
            // String[] words = data.split(" {34}");
            for (String a : words) {
                StringBuilder builder = new StringBuilder(a);
                // why while loop is required?
                //while ((sb.read()) != -1) {
                    output.write(builder.reverse().toString());
                    output.flush(); // flush data to the file
                //}
            }
        }
        output.close();
    }
}

Read about File writer here on how to flush data and also close the writer after writing is completed.



Answered By - Prog_G
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing