Re: Number format exception
Venki wrote:
Can any one pls fix dis runtime error
"pls" and "dis" are not English words.
To what runtime error to you refer? What is the exact text?
/* Dealing with binary data:
Create a program to read a list of floating point numbers in a text
file and then write the data to a binary file.
(Use classes: FileReader, FileBufferedReader, FileOutputStream,
String, Float, DataOutputStream).
Note:
Use FileReader and FileBufferedReader classes to be able to read each
line of the file as a string. Use split method of String class to
break the line into words.
Use Float.parseFloat to convert the string to floating point number.
Use FileOutputStream and DataOutputStream to write the float data as a
binary data to the file.
*/
Okay, so we know what your homework is. Are there teaching assistants
or a teacher who can provide you with advice?
import java.io.*;
import java.util.regex.*;
public class readfloat {
Type names should start with an upper-case letter, as should every
compound word part, e.g., 'ReadFloat'. Even better, the type name
should function as a noun: 'FloatReader'.
public static void main(String args[]) throws IOException {
The main routine should not throw a checked exception.
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
String s;
Pattern pat = Pattern.compile(", ");
FileOutputStream fout = new FileOutputStream("output.tx=
t");
DataOutputStream out = new DataOutputStream(fout);
while((s = br.readLine())!=null) {
String str[] = pat.split(s);
for(int i=0;i<str.length;i++){
float f = Float.parseFl=
oat(str[i]);
What happens here if the parsed string 'str[i]' does not contain
characters appropriate for a 'float'? How would you know if that
happened, and what the unexpected input was?
out.writeFloat(f);
}
}
fr.close();
out.close();
}
--
Lew