read and modify a text file
Good evening \ morning ppl
i have homework that requires
"Write a Java program to perform the following:
Read 5 floating point number and 5 integers from Keyboard.
Write them to a file with name dataFile1.txt
Read and display them.
Read and multiply by 2 and write them back to the another file dataFile2.txt.
Read the file dataFile2.txt and display the values"
I did the first part but I have difficulties in doning the second part which is "Read and multiply by 2 and write them back to the another file dataFile2.txt."
Can any one help me
Re: read and modify a text file
What are you having problems with?
Please explain.
Re: read and modify a text file
You should probably use a Scanner, or something to read the file.
It has a nextInt() function already in it.
However, you should probably keep track of which type is a int and which is a float and where they are. For instance, you could do something like this
Scanner s;
try
{
s = new Scanner(new File("dataFile2.txt"));
}
catch(java.io.FileNotFoundException fnfe)
{
System.out.println("File not found.");
System.exit(1);
}
Did you try that already?
Also, you could try this
int x;
float y;
x = s.nextInt();
y = s.nextFloat();