Richard Teasdale
asked on
convert string array to number
Hi: I am using Java to amend a text file. There are 80 fields (csv) in the file so I have split into 80 string arrays.
However, one amendment requires me to subtract one array from another. It is the last amendment I need to do!
Does anybody know how to change a string array in java to a number?
I attach the csv file I am trying to amend, plus the java I have written so far. Don't laugh; I am just a novice at this stuff.
Line 166 in the java is where i have 'added' the two arrays - String d4 = (x[62])+ (x[63]);
I need to subtract!
Thanks!
1907D45C.CSV
filesweeper.txt
However, one amendment requires me to subtract one array from another. It is the last amendment I need to do!
Does anybody know how to change a string array in java to a number?
I attach the csv file I am trying to amend, plus the java I have written so far. Don't laugh; I am just a novice at this stuff.
Line 166 in the java is where i have 'added' the two arrays - String d4 = (x[62])+ (x[63]);
I need to subtract!
Thanks!
1907D45C.CSV
filesweeper.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi : thanks for your input so quickly!
I have looked at Krakatoa's suggestion as follows:
basic attempt - just to test the process with a fixed string:
Integer poo = Integer.valueOf("1,000".re placeAll(" ,", "")); //..........returns 1000...yay! we are on the way!
String d2 = (x[62]); //...............where d2 is "290.0"
Integer poo = Integer.valueOf(d2.replace All(",", "")); //.........returns:
Exception in thread "main" java.lang.NumberFormatExce ption: For input string: ""290.0""
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.java:65 )
at java.lang.Integer.parseInt (Integer.j ava:481)
at java.lang.Integer.valueOf( Integer.ja va:582)
//....ow, poo.
get rid of " marks:
String d2 = (x[62].substring(1,6)); //(where d2 is 290.0)
Integer poo = Integer.valueOf(d2.replace All(",", "")); //.....returns:
Exception in thread "main" java.lang.NumberFormatExce ption: For input string: "290.0"
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.java:65 )
at java.lang.Integer.parseInt (Integer.j ava:492)
at java.lang.Integer.valueOf( Integer.ja va:582)
//...so I have still got some " marks?
Sorry if I am being thick.
CEHJ - sorry but I misunderstand. DO you mean the number of rows in the source file? anything, really. It is a purchase order from a customer.
I have looked at Krakatoa's suggestion as follows:
basic attempt - just to test the process with a fixed string:
Integer poo = Integer.valueOf("1,000".re
String d2 = (x[62]); //...............where d2 is "290.0"
Integer poo = Integer.valueOf(d2.replace
Exception in thread "main" java.lang.NumberFormatExce
at java.lang.NumberFormatExce
at java.lang.Integer.parseInt
at java.lang.Integer.valueOf(
//....ow, poo.
get rid of " marks:
String d2 = (x[62].substring(1,6)); //(where d2 is 290.0)
Integer poo = Integer.valueOf(d2.replace
Exception in thread "main" java.lang.NumberFormatExce
at java.lang.NumberFormatExce
at java.lang.Integer.parseInt
at java.lang.Integer.valueOf(
//...so I have still got some " marks?
Sorry if I am being thick.
CEHJ - sorry but I misunderstand. DO you mean the number of rows in the source file? anything, really. It is a purchase order from a customer.
CEHJ - sorry but I misunderstand. DO you mean the number of rows in the source file? anything, really.Yes. I'm guessing that it will never be too large to hold in memory all at once, so getting String[][] should not be a problem. You'll see i've added to my original comment
ASKER
Actually, CEHJ, you have spotted the main flaw in my project. What you are saying is that if the customer puts a comma in any text field then the whole thing fails? Damn. Never thought of that. Thanks! Not sure what you mean by a solid csv library; I have just knocked this up in netbeans using my old friends google...and you, of course! Maybe a rethink is on the cards. Or a little sob.
ASKER
Hi guys:
Sorted, thanks to you both!
Double one = Double.valueOf(d22.replace All(",", ""));
Double two = Double.valueOf(d23.replace All(",", ""));
Double three = (two-one);
Thanks you very much , I have to award the points to CEHJ as he gave the final code and even though he demonstrated my whole attempt is futile!
But thanks to Krakatoa for your response. Greatly appreciated.
Sorted, thanks to you both!
Double one = Double.valueOf(d22.replace
Double two = Double.valueOf(d23.replace
Double three = (two-one);
Thanks you very much , I have to award the points to CEHJ as he gave the final code and even though he demonstrated my whole attempt is futile!
But thanks to Krakatoa for your response. Greatly appreciated.
ASKER
Thanks to you both for your help. Much appreciated.
You need to parse the (numeric) fields on which you want to do math, using the correct wrappers, so for the two fields above causing problems you can parse them as Floats.
As for your delimiters - I can't see how a comma-delimited CSV file would be generated that would mistake a field-level "," for a delimiter of the same kind - unless the CSV file is coming from a parsing algo that is wrong. Other apps should allow you to choose your delimiters.
As for your delimiters - I can't see how a comma-delimited CSV file would be generated that would mistake a field-level "," for a delimiter of the same kind - unless the CSV file is coming from a parsing algo that is wrong. Other apps should allow you to choose your delimiters.
Right, well you can be sure I won't be parsing your way again in the foreseeable. Thanks for nothing.
Opencsv supports all the basic CSV-type things you’re likely to want to do:(my emphasis)
Arbitrary numbers of values per line.
Ignoring commas in quoted elements.
http://opencsv.sourceforge.net/
It is possible to split points for the question btw
As for your delimiters - I can't see how a comma-delimited CSV file would be generated that would mistake a field-level "," for a delimiter of the same kindIt's not the file generation that is the problem, it's the parsing of it.
It's not the file generation that is the problem, it's the parsing of it.
Yes, understood; that’s my point. ;) What my comment implies however is that if the CSV is itself mal-generated by parsing some form of text file without due consideration to the role(s) of commas and double quote marks, then that would be a problem.
Yes. If it's done by a spreadsheet program like Excel or Calc then it's going to be OK though. The point is that you can't (safely) parse csv by using String.split
Abso, yes.
Well I doubt you mean an array if the item is a single field - isn't it a simple String you mean ?
If so, that field can be captured numerically by Integer.valueOf(String), from which you can obtain an int primitive if required. If you are doing maths on two such fields, then it should be straightforward from there.
incidentally, it might help us and encourage other experts to drop in, if you could relieve some of the tedium of having to parse your CSV file visually to determine where records start and end, and give us just one complete record as a core example.