Skip to main content

I am trying to write to a file using BufferedWriterBufferedWriter. As the write method takes intint argument, I am casting input doubleof double type into intint 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 input as int that, file.nextInt() but it does not work either. 
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.

I am trying to write to a file using BufferedWriter. As the write method takes int argument, I am casting input 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 input as int that, file.nextInt() but it does not work either. 

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.

Source Link

How can I write to a file using BufferedWriter if the input type is double?

I am trying to write to a file using BufferedWriter. As the write method takes int argument, I am casting input 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 input as int that, file.nextInt() but it does not work either.