Question

In java, how can I convert this text file format: n=6 m=7 1 2 5 4...

In java, how can I convert this text file format:

n=6 m=7
1

    2    5
    4    9
    5    6

2
    3    2

3
    4   3

5
    6    2

6
    4    2

// end of file

into this text file format:

1,2,5
1,4,9
1,5,3
2,3,2
3,4,3
5,6,2
6,4,2

 

This is how to read the first text file:

The first line of each file below contains the number of vertices and the number of edges in the graph (in the format "n=XXXX m=XXXXX"). The rest of the file is organized as follows: each vertex i appears on a line by itself, followed by a line for each neighbor j>i of i (containing j and the length of edge (i,j)). Each list of neighbors is ended by an empty line. Vertices i which do not have neighbors with an index greater than i are not represented. NOTE: Vertices are indexed from 0 to n-1. NOTE: each edge is mentioned only once with its smaller number endpoint Example: vertex 1 will have vertex neighbors 2, 4, and 5 with edges 5, 9, 6 respectivly.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

public class FileConversion {

   public static void main(String[] args) throws IOException {

       BufferedReader br = new BufferedReader(new FileReader("D:\\file1.txt"));
       try {
           //String builder for creating format
           StringBuilder sb = new StringBuilder();
           //Read line by line from text file
           String line = br.readLine();
           String ver = null;
           while (line != null) {
               //split each and every line by spaces
               String[] strLine = line.toString().split(" ");
               // if line has edge
               if (strLine.length == 1 && !strLine[0].equals("")) {
                   ver = strLine[0];
               }
               //if line has vertex
               else if (ver != null && strLine.length != 1) {
                   String temp = ",";
                   for (String str : strLine) {
                       //Condition to ignore white spaces
                       if (!str.equals("")) {
                           temp = temp + str + ",";
                       }
                   }
                   sb.append(ver + temp.substring(0, temp.length() - 1));
                   sb.append(System.lineSeparator());
               }

               line = br.readLine();

           }
           String newContent = sb.toString();
          
           //Write the new content to file
           BufferedWriter bw = null;
           FileWriter fw = null;
           try {
               fw = new FileWriter("D:\\file2.txt");
               bw = new BufferedWriter(fw);
               bw.write(newContent);
           } catch (IOException e) {

           }finally{
               bw.close();
               fw.close();
          
           }

       } finally {
           br.close();
              
       }

   }

}

Add a comment
Know the answer?
Add Answer to:
In java, how can I convert this text file format: n=6 m=7 1 2 5 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT