I need some help regarding converting string array list to double array list using java lambdas upto now I had tried these.
List<Double> Doublevalues1 = valueItemsToList.stream().mapToDouble(Double::parseDouble).boxed().collect(Collectors.toList());
and this in general
for(int j = 0; j< valueItemsToList.size();j++)
{
Doublevalues1.add(Double.parseDouble(valueItemsToList.get(j)));
//Doublevalues.add(Double.valueOf(valueItemsToList.get(j)));
}
can anyone tell me where I gone wrong This is not duplicate answer I tried all possibilites I had gone through the net but none of them gave me correct answer glad if you could help. this is my complete code
public class Stock {
public static void main(String[] args) {
// TODO code application logic here
String buff[] = new String[4988];
int i = 0;
File file = new File("C:\\Users\\admin\\Desktop\\data.csv");
try{
FileInputStream fis = new FileInputStream(file);
BufferedReader dis = new BufferedReader(new InputStreamReader(fis));
String s;
while ((s = dis.readLine()) != null) {
// System.out.println(s);
buff[i] = s;
i++;
// String[] s1 = s.split(",");
}
}
catch(Exception e)
{
System.out.println("error happend at block try at fileinput");
}
//converting array string to arraylist
List<String> valueItemsToList = Arrays.asList(buff);
//valueItemsToList.forEach(System.out::println);
List<Double> Doublevalues1 = new ArrayList<Double>();// valueItemsToList.stream().mapToDouble(Double::parseDouble).boxed().collect(Collectors.toList());
for(int j = 0; j< valueItemsToList.size();j++)
{
Doublevalues1.add(Double.parseDouble(valueItemsToList.get(j)));
//Doublevalues.add(Double.valueOf(valueItemsToList.get(j)));
}
Doublevalues1.forEach(System.out::println);
}
}
error: its little bit clumsy its here complete
Exception in thread "main" java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at stock.Stock.main(Stock.java:53) C:\\Users\\admin\\AppData\\Local\\NetBeans\\Cache\\8.1\\executor-snippets\\run.xml:53: Java returned: 1 BUILD FAILED (total time: 2 seconds)
Agree with @bayou.io, either use ArrayList<String>
in place of buff[]
(because if the number of lines in your input file are less 4988 then you will get NullPointerExcetion and if you have more lines then you will get ArrayIndexOutOfBoundsException)
or place null
check before String to Double conversion eg
for(int j = 0; j< valueItemsToList.size();j++)
{
if(valueItemsToList.get(j) != null){ // NULL Check
Doublevalues1.add(Double.parseDouble(valueItemsToList.get(j)));
//Doublevalues.add(Double.valueOf(valueItemsToList.get(j)));
}
}
List<String> valueItemsToList = Arrays.asList(buff);
//valueItemsToList.forEach(System.out::println);
List<Double> Doublevalues1 = new ArrayList<Double>();// valueItemsToList.stream().mapToDouble(Double::parseDouble).boxed().collect(Collectors.toList());
for(int j = 0; j< valueItemsToList.size();j++)
{
Doublevalues1.add(Double.parseDouble(valueItemsToList.get(j)));
//Doublevalues.add(Double.valueOf(valueItemsToList.get(j)));
}
Doublevalues1.forEach(System.out::println);
solved thanks for your posts
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.