Question:
Cannot write Integer values into a File using Java?
?
2009-05-08 01:37:25 UTC
I had wriiten Code in Java to get input from Console and write that input into a File.But i cant write Integer Values into the File.
Four answers:
deonejuan
2009-05-08 01:52:56 UTC
If you are using FileWriter, that does use text. The usual practice is to prepend a blank String in front of the number, eg.



int [] nums = { 3,1,2,55,6 };



FileWriter fout = new FileWriter("test.txt"); // fileName

for( int i = 0; i< nums.length; i++ )

fout.write(""+nums[2]);



fout.close();

-- or --

use the method of String

fout.write( String.valueOf( nums[0]) + " ");

-- or --

fout.write( String.format("%d ",nums[1] );



========

See, this allows us to 'format' the file with the delimeter and the line ending. If you were using a StreamWriter, that's binary and that requires other thinking, but you don't have to convert to String.
2016-12-15 19:34:31 UTC
Java Write Int To File
?
2016-04-08 15:27:47 UTC
For the best answers, search on this site https://shorturl.im/axYnQ



This seems like your just having trouble understanding how to structure your program. Without actually writing any code for you(You should do this yourself so you can learn) lets try and understand what it is your program is actually supposed to do 1.) Reads in a string from the user I am not sure what the lue.nextLine() is I am assuming that lue is the name of a file. From what your teacher said it doesn't mention anything about files it just says read input from the user. This is done traditionally with the scanner object(I'll leave it to you to look up how it works) If you do need to use a file the logic below would work the same expect it would need to be done for each value in the file 2.) So once we have our string we need to determine whether or not it is a number or a string sounds to me like we have a "decision" to make than based on that we determine what to do next 3.) If it is a string we should re-prompt the user until they enter a number then check if the number is in the correct range once they have 4.) If it is initially a number we just need to make sure its withing the correct range if not reprompt till they enter something in the appropriate range Question: Does it appear to you that both steps 3 & 4 share some of the exact same logic namely check to see if the number is in the right range reprompt if not. What should we do if we are repeating the same exact code in 2 different parts of our program sounds to me like we should right a function. So lets recap prompt the user get the input based on the input do one of 2 things both of which share some similair atttributes. I encourage you to use what I've said here and implement the solution on your own. I know that it may be tempting to use code written by others that may reply to your question but if you do you take away none of the problem solving skills gained from doing it on your own you may say so what no big deal its just one piece of code but let me tell you people who do this do not become good programmers this is easy code for me to figure out but only because when I was like you(little experience) I put the time and effort into learning how to program. I'll let you in on a little secret that most programmers don't find out until they realize they are not fit to program and have to persue something else. Being a good programmer has very little to do with syntax learning the in and outs of a language is no where near as important as problem solving that is what program is your given a problem and ask to solve it with a computer if you cannot do that you will not make it as a programmer I know because I have seen it happen to people
radhi
2009-05-08 03:15:53 UTC
OutputStreamWriter..

write(int)

write(char[],int,int)

write(String,int,int)



where did your see

write(char)?



> What am I supposed to do?



Read the docs carefully.







And ..since this is c.l.j.programmer, you

_do_ know where to go from here, right?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...