I am trying to write to a file using BufferedWriter. As the write method takes int argument, I am casting input of double type into int type but it is not being written properly into the file.
try{
Scanner file = new Scanner(new File("/home/amit/Desktop/number.csv"));
file.useDelimiter(", *");
BufferedWriter writer = new BufferedWriter(new FileWriter("/home/amit/Desktop/output_number.csv"));
while(file.hasNextDouble()){
writer.write((int)(file.nextDouble()));
}
writer.flush();
writer.close();
}catch(Exception x){
x.printStackTrace();
}
I tried reading the input as int that, file.nextInt() but it does not work either.
BufferedWritercannot writedoubles, see the API specification. However, you can convert yourdouble (file.nextDouble())into aString, for instance usingDouble.toString((file.nextDouble()))and then write the resulting string withwrite(String).