java - how do u read all doubles from txt file? -
It's easy when there is a file of all doubles, but when a non-double is somewhere in the middle, then I It does not enable it to catch all of them.
For example:
604.2 609.2 6042 604.4 4234.324 312 gfsdgfreg 6043 604.3
Output:
604.2 609.2 6042.0 604.4 4234.324 312.0
Obviously, two pairs are missing, is there any way to catch them all , Which use the Hydicted Double ()? If thx in advance u do not receive a reply. I have seen somewhere that I can parse each of them for doubling the exception and catching, but I'm not really advanced
What's here with me:
import java util *. Import java.io * *; Public Class Lab11 {Public static zero main (string [] args) FileNotFoundException throws {file nums = new file ("file.txt"); Int size = arrSize (nums); Double [] phonenum = copy (nums, size); (Int i = 0; I & lt; Phone no. Lamp; I ++) system. Out.printLN (phoneme [ii]); } Public static arrSize (file f) throws FileNotFoundException {int arrSize = 0; Scanner = new scanner (f); While (in.hasNextDouble ()) {arrSize ++; Next (); } In.close (); Return arrSize; } Public Static Double [] Copy (File F, Integer Size) FileNotFoundException throws {new scanners (F) in scanner; Double [] list = new double [size]; Int i = 0; While (in.hasNextDouble ()) {list [i ++] = in.nextDouble (); } In.close (); Return list; }}
I will give you two of the following structures:
while (in.hasNext ()) {if (in.hasNextDouble ()) {// loop code in your internal time} else {in.next (); }}
Otherwise, you will remember everything after the first example of non-double.
Comments
Post a Comment