简体   繁体   中英

Java text file remove doubles from four column text file

I have to write a code to take a text file, which contains integers and doubles, and to print the doubles in a file and the integers in another one. The text file is formatted in the following way:

double int int int  
double int int int  
...  
double int int int

It is saved in the "raw.txt" file.

The output should look like:

int int int  
int int int   
...  
int int int  

This is what I have tried so far:

import java.io.*;
import java.util.*;

public class DATA {
    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter writer = new PrintWriter(new File("sorted.txt"));
        Scanner reader = new Scanner(new File("raw.txt"));
        int temp = 0, count = 1;
        while (reader.hasNext()) {
            try {
                temp = reader.nextInt();
            }
            catch (InputMismatchException e) {
                reader.nextLine();
                temp = (int) reader.nextDouble();
            }
            writer.print(temp);
            if (count % 4 == 0) 
                writer.println(); 
            count++;    

        }
        writer.close();
        reader.close();
    }
}

The current code throws an InputMismatchException . All help is greatly appreciated.

Based on your provided code you only want to split the files and do not care about the double and int values itself. So you could handle the file as a normal text file and split the values by the separating blank character.

The snippet does some assumptions on the format of the raw.txt file and is not optimised. So it should be an easy task to amend it based on your needs.

public static void main(String[] args) throws IOException {
    List<String> rawLines = Files.readAllLines(Paths.get("raw.txt"));
    try (Writer intWriter = Files.newBufferedWriter(
            Paths.get("int_values.txt"),
            StandardOpenOption.CREATE_NEW);
         Writer doubleWriter = Files.newBufferedWriter(
            Paths.get("double_values.txt"),
            StandardOpenOption.CREATE_NEW)) {
        for (String line : rawLines) {
            // the split might need to be amended if the values
            // are not separated by a single blank
            String[] values = line.split(" ");

            // to be amended if there are not alway four values in a row
            if (values.length != 4) {
                continue;
            }

            doubleWriter.write(values[0]);
            doubleWriter.write(System.lineSeparator());

            intWriter.write(values[1]);
            intWriter.write(' ');
            intWriter.write(values[2]);
            intWriter.write(' ');
            intWriter.write(values[3]);
            intWriter.write(' ');
            intWriter.write(System.lineSeparator());
        }
    }
}

InputMismatchException can be thrown because it is nether Integer either Double
It is much better to read a part as a String and then decide
When it is deciding, it throws NumberFormatException which can be catched
In following code there are two writers separated as you wanted, It could looks better than this code maybe
I have corrected your writing to file. I havent tested it but I really think if you do writer.print(temp); , it will put all integers without spaces, which is useless then.
Try this code, but not tested

import java.io.*;
import java.util.*;

public class DATA {
     public static void main(String[] args) throws FileNotFoundException {
        PrintWriter writerInt = new PrintWriter(new File("sortedInt.txt"));
        PrintWriter writerDou = new PrintWriter(new File("sortedDou.txt"));
        Scanner reader = new Scanner(new File("raw.txt"));
        int temp = 0, countInt = 1, countDou = 1;
        while (reader.hasNext()) {
            String next = reader.next();
            try{
                temp=Integer.parseInt(next);
                writerInt.print(temp+" ");
                if (countInt % 4 == 0) 
                    writerInt.println(); 
                countInt++;    
            }catch(NumberFormatException e){
                try{
                    writerDou.print(Double.parseDouble(next)+" ");
                    if (countDou % 4 == 0) 
                        writerDou.println(); 
                    countDou++;   
                }catch(NumberFormatException f){
                    System.out.println("Not a number");
                }
            }

        }
        writerInt.close();
        writerDou.close();
        reader.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM