Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Migration and Modernization
E-Business Suite
PeopleSoft Enterprise
JD Edwards World
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums



New To Java

Split double numbers from a text file into double array

I have the following code: import*; public class x { private static String readFileAsString(String filePath) throws{     byte[] buffer = new byte[(int) new File(filePath).length()];     BufferedInputStream f = new BufferedInputStream(new FileInputStream(filePath));;     return new String(buffer); }     public static void main(String[] args) throws{         String str2array = readFileAsString("x.txt");         String[] arr=str2array.split(","); String a = arr[5]; System.out.println(a); double b = arr[5]; System.out.println(b);     } }  and x.txt: 0.03955945,0.03955945,0.11761671,0.057252668,0.030778322,0.017218199,0.0124009065,0.0124009065,0.0047305133,0.0047305133,0.0047305133,0.0032182655,0.003978315,0.07595109,0.089649245,0.089649245,0.026411638,0.01330624,0.005414539,0.004363436,0.004363436,0.0042371973,0.038139768,0.038139768,0.101917334,0.06477121,0.033142086,0.017457731,0.017457731,0.009107639,0.009107639,0.0071107424,0.004103866,0.0040783533,0.0040783533,0.005235885,0.06375461,0.08578691,0.08578691,0.02458044,0.01838197,0.01838197,0.008729034,0.008729034,0.006332842,0.006332842,0.004833875,0.0030726937,0.002600148,0.0030155652,0.088991135,0.088991135,0.088991135,0.01730314,0.01730314,0.0067671454,0.038042143,0.03816381,0.03816381,0.0230084,0.010566873,0.01911031,0.01911031,0.01911031,0.01835568,0.0138946995,0.011414777,0.008515169,0.008289955,0.008289955,0.009237138,0.00809182,0.005245238,0.0058210404,0.0058210404,0.03706791,0.089868285,0.089868285,0.043346766,0.016818156,0.016818156,0.0044978005,0.054433424,0.03563282,0.02209424,0.02209424,0.012800509,0.029410426,0.0258746,0.011521634,0.011521634,0.008123339,0.0073138005,0.00673923,0.00673923,0.0056050676,0.007074402,0.007445267,0.007445267,0.007666857,0.0070602125,0.0070602125,0.08097361,0.12492809,0.12492809,0.106743686,0.087920606,0.08924122,0.08924122,0.057736725,0.041736055,0.028767848,0.028767848,0.024077855,0.014412839,0.014412839,0.012894028,0.013346321  The result, when I run the program: incompatible types found   : java.lang.String required: double double b = arr[5];   I want to split double numbers from x.txt file into double array. My solution is wrong, because I use String[] arr=str2array.split(","); I tried to change String[] arr=str2array.split(","); to Double[] arr=str2array.split(","); but, don't work. Can someone help me?

Obviusly you have to parse each String into a double.

flounder wrote: Obviusly you have to parse each String into a double. Yes, You're right. I used: Double.parseDouble(arr[i]);  Your reply is correct. Thank you very much!

I should have mentioned an alternative.  If you have Java 1.5 and above then you could use the Scanner class which has the nextDouble method which will read and return the next double from your file.

Note: This thread was originally posted in the [Java Programming|] forum, but moved to this forum for closer topic alignment.

Related Links

Problem with USERNAME & PASSWORD creation--JDBC connection with MYSQL
it runs but the answer i get is wrong:(
how do I set color in IndexColorModel for Graphics
ERROR: ...
boolean in method
Java and COM
Errors from Java to DB2
Exception Handling - Doubt
package access
Int to String
Help with array method errors
Static Methods - Usage?
JList ?
need help with while loops
How do I go to any subdirectory in my JDK installation?
Auto refresh..