Introduction
I made a a simple program that stores data in a .csv file format which later reads to plot. Everything done in Java.
An example from the data from the csv file is:
2018/12/29
Tejido,321 908,13.55,43.18,$15.98,
Ropa,195 045,20.55,45.93,$123.01,
Gorra de visera,126 561,17.43,42.32,$79.54,
Cerveza,80 109,3.37,17.93,$12.38,
Mercancías de playa,75 065,11.48,39.73,$105.93,
Bebidas alcohólicas,31 215,4.84,27.90,$32.29,
Artículos de cuero,19 098,23.13,44.09,$198.74,
What I have tried so far?
After reading and research in the docs I came up with this solution which quite adapts to my problem (if it worked...)
public class CSVinput {
public static void main(String[] args) throws FileNotFoundException
{
Scanner scan = new Scanner(new File("produccion.csv"));
scan.useDelimiter(",");
while(scan.hasNext())
{
String date = scan.next();
System.out.println(date);
String name = scan.next();
System.out.println(name);
int quantity = Integer.parseInt(scan.next().replaceAll(" ", "."));
System.out.println(quantity);
double quality = Double.parseDouble(scan.next());
System.out.println(quality);
double realmQ = Double.parseDouble(scan.next());
System.out.println(realmQ);
double cost = Double.parseDouble(scan.nextLine());
System.out.println(cost);
if (scan.hasNextLine())
{
scan.nextLine();
System.out.println(date+"," + name+"," + quantity+"," + quality+"," + realmQ+"," + cost);
}
scan.close();
}
}
}
Where is the problem?
The problem is when I try to import the String data and cast it into double/float which then throws at me:
Exception in thread "main" java.lang.NumberFormatException: For input
string: "13.55"
I though that if I parsed it into double it would be more than enough to get it right.
Full Exception Error
Exception in thread "main" java.lang.NumberFormatException: For input string: "13.55"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at webscraper.CSVinput.main(CSVinput.java:29)
C:\Users\Jonathan\Desktop\WebScraper_03\WebScraper\nbproject\build-
impl.xml:1339: The following error occurred while executing this line:
C:\Users\Jonathan\Desktop\WebScraper_03\WebScraper\nbproject\build-
impl.xml:980: Java returned: 1
BUILD FAILED (total time: 1 second)