Read Double From a .txt File into an Array List

Read Double From a .txt File into an Array List



So we have this program where we have to read double values from a text file into a double ArrayList and make certain calculations.



example of the text file:



Note: there's space between the date and the double values


5/05/1998 4.4

1/01/1999 -4.123



the problem is that i'm getting is as below:



"NumberFormatException for input string: 1/2/1950 0.0258" error.



Here's my code:


public static void value() throws java.io.IOException
try
BufferedReader br = new BufferedReader(new FileReader(new File("SP500-Weekly.txt")));
ArrayList<Double>list = new ArrayList<Double>();
String line;
while((line=br.readLine())!=null)
String r = line.split(" ");
for(int i=0; i<r.length; i++)
double val = Double.parseDouble(r[i]);
list.add(val);

br.close();
System.out.println(list.size());

catch(IOException io)
System.out.println("error");






Can you format the value in text file to be exactly what you are trying to load? Are each record present in new line?
– stackFan
Sep 4 '18 at 21:07





yes every record is in a new line.
– Shahbaz Malik
Sep 4 '18 at 21:09





And what is your output supposed to be like? It is all doubles values in a single list?
– stackFan
Sep 4 '18 at 21:11





Why are you splitting by 4 spaces? Shouldn't you split by single space and read only the second part of the result?
– Loris Securo
Sep 4 '18 at 21:12






@stackFan, the doubles should be stored in a arraylist and then we have to access one of those randomly using Double value = data.get(r.nextInt(data.size()));.
– Shahbaz Malik
Sep 4 '18 at 21:13




3 Answers
3



Seems like you just need the double value present on each line and want to add it into a List<Double>. Based off of your input, second value is always your double value, but you are trying to parse first value as well which is a date and Double.parseDouble() cannot parse characters that comes in a date such as /, thus you ran into exception. If the double value is what you need then do it simply as :


double


List<Double>


Double.parseDouble()


/


try
BufferedReader br = new BufferedReader( new FileReader( new File( "SP500-Weekly.txt"" ) ) );
List<Double> list = new ArrayList<Double>();
String line;

while ( ( line = br.readLine( ) ) != null )

String r = line.split( "\s+" );
list.add( Double.parseDouble( r[ 1 ] ) );

br.close( );
System.out.println( list.size( ) );
catch ( IOException io )
e.printStackTrace( );



You can then play around with list variable as your wish.


list



Also if you want to be more specific and check if it's actually number/double for second value then I'd do following to add a regex which will make sure that the second value is number (with or without - sign) as below:


-


while ( ( line = br.readLine( ) ) != null )
String r = line.split( "\s+" );
for ( String string: r )
if ( string.matches( "^-?[0-9]?.?[0-9]+" ) )
list.add( Double.parseDouble( r[ 1 ] ) );







Closing your resources manually or yet better using a try with resources would have been very nice. Also coding to the implementation rather than the interface is not correct.
– Aris_Kortex
Sep 4 '18 at 21:18





That worked! I've posted the "correct code!"
– Shahbaz Malik
Sep 4 '18 at 21:23





@Aris_Kortex thanks for suggestion. Initially I wrote to address the main issue OP was having. Now the code has been updated.
– stackFan
Sep 4 '18 at 21:53




A very simple and up to date implementation of this using java.nio and pure Java 8 would the following.


java.nio


private static List<Double> readDoubles(final String fileName)
try (Stream<String> stream = Files.lines(Paths.get(fileName)))
return stream.map(s -> s.split("\s+")[1])
.map(Double::parseDouble)
.collect(Collectors.toList());
catch (IOException e)
e.printStackTrace();

return Collections.emptyList();



Implementation also takes the liberty of using try-with-resources to auto manager the various resources.


try-with-resources



CORRECTCODE:


import java.util.List;
class Sample extends SimulateMarket
public static void value()
try
BufferedReader br = new BufferedReader(new FileReader(new File("SP500-Weekly.txt")));
List<Double> list = new ArrayList<Double>();
String line;
while((line=br.readLine())!=null)
String r = line.split("\s+");
list.add(Double.parseDouble(r[1]));
br.close();
Random rand = new Random();
int randomIndex = rand.nextInt(list.size());
System.out.println(list.get(randomIndex));

catch(IOException io)
System.out.println("error");








This also obviously wrong. You catch and throw the same exception. Either do one of them. Also you're still using the concrete implementation for the initialization of the list.
– Aris_Kortex
Sep 4 '18 at 21:33





The catch/throw thing i've fixed; however, i'm learning java basics at my college and i'm not familiar with the term "concrete implementation."
– Shahbaz Malik
Sep 4 '18 at 21:46






This is referring to this ArrayList<Double>list = new ArrayList<Double>();. Use this List<Double> list = new ArrayList<Double>(); instead.
– Aris_Kortex
Sep 4 '18 at 21:48


ArrayList<Double>list = new ArrayList<Double>();


List<Double> list = new ArrayList<Double>();





I fixed my code above! :)
– Shahbaz Malik
Sep 4 '18 at 21:57



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế