Would love to see you guys' input on this one :-|
I kinda like this approach better than the double for loop in the solution...
package com.codegym.task.task19.task1908;
/*
Picking out numbers
*/
import java.io.*;
public class Solution {
private static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException, FileNotFoundException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String f1 = br.readLine();
String f2 = br.readLine();
BufferedReader fr = new BufferedReader(new FileReader(f1));
BufferedWriter fw = new BufferedWriter(new FileWriter(f2));
String str = "";
while(fr.ready()){
char c = (char) fr.read();
if(c == ' '){
str = sb.toString(); // "12"
isNumeric(str, fw, false);
sb = new StringBuilder();
} else {
sb.append(c);
}
}
str = sb.toString();
isNumeric(str, fw, true);
fr.close();
fw.close();
br.close();
}
public static void isNumeric(String str, BufferedWriter fw, boolean isLast) {
if (str == null) {
return;
}
try {
double d = Double.parseDouble(str);
if(isLast){
fw.write(str + " ");
} else {
fw.write(str + " ");
}
} catch (NumberFormatException nfe) {
} catch(IOException io){
}
}
}