0

well what i get is that "the file has been added/written to the arraylist.

0

so if you print out the content of the arraylist now you get the numbers that are in the file?

Edited by Slavi

0

no i dont.. ill try again put it in an new class without all the surrounding code and see if it changes anything. man this is soooo frustrating :)

0

In my opinion the program shouldn't get to the point of NaN. You should check that the array contains data before trying to use it for computations.

I believe that the real issue may be the locale and input settings on your computer. In some European countries, the comma "," is used instead of decimal ".". Check the input language and locale settings on your computer. You may need to change the "." (decimal) to "," (comma) in "orange.csv"

Greek Numbers

..."Decimals: in Greek, the use of commas and periods is different than in English."

"Period is used to separate the thousands, while the comma is used as the decimal point."

Edited by cgeier

0

thank you, ill try that.

btw, did you send that greek numbers link?

0

What do you mean by "...did you send that Greek numbers link?" It's something I found when searching for the decimal separator for Greek.

Edited by cgeier

1

Line 54, are you sure that you can add double into ArrayList<Double>??? Oh and you could try to use File.separator instead of using slash/backslash...

PS: How about read in as "String" using nextLine() instead? I suggested a long time about about it. This way, you could verify and easily manipulate the string to double.

Edited by Taywin

0

yeah well i didnt quite understand why you sent it. Im greek but born and raised in Sweden :-) but thanks anyway

0

Some of your comments in your program were in another language and your profile shows your location as Greece. Didn't really look like Greek, but I don't speak Greek so...

What country is the locale set to for the computer that you are using?

Edited by cgeier

0

ah i see..my comments are in swedish. You are right, greek is different, it has an other alphabet :)
The locale is set to swedish

0

taywin, thanks for the tip but i was using nextline before, also had a string called line for that earlier but my teacher thought it was a waste of code to parse the array to double after.
And iyes, i can add double into arraylist double, there is no problem about that from what ive seen on other programs. Thanks tho. Ive asked my teacher to look it through if he can see whats wrong. Im going nuts. Thanks for your efforts

0

I suspect the Swedish locale uses the comma as the decimal separator. Your file has . as the decimal separator, so that may be causing a problem in reading floats from the file.
You could try setting your Scanner's locale to US, eg

input.useLocale(Locale.US);
0

I have been able to replicate NaN in your originally posted code by the following.

Using locale = "en_US" -- English (United States), change numbers (in "orange.csv") to use "," (comma) instead of "." (decimal).

Using locale = "sv_SE" -- Swedish (Sweden), change the numbers (in "orange.csv") to use "." (decimal) instead of "," (comma).

To change locale:
Win 7:
-Start
-Control Panel
-View By: Small Icons
-Region and Language
-Click "Formats" tab
-For "Format", select desired format -- ex: English (United States) or Swedish (Sweden).
-Click "Additional settings" button
-Click "Numbers" tab
-Look at value for "Decimal symbol"

Note: English (United States) only has one choice -- "." Swedish (Sweden) has choice between "." and ","

To find out what the decimal separator on your computer is:

Add the following import statement:
import java.text.DecimalFormatSymbols;

Then:

DecimalFormatSymbols dfs = new DecimalFormatSymbols(); 
System.out.println("Decimal separator for your locale: " + dfs.getDecimalSeparator());

Resources:
Java 7 default locale

Decimal Separator in NumberFormat

Edited by cgeier

1

Yes, here's the code I used to confirm the problem...

       String s = "1.2 3.4";
       Scanner input = new Scanner(s); // my default Locale is UK
       System.out.println(input.locale() + " " + input.nextDouble());

       Locale.setDefault(new Locale("sv", "SE"));
       input = new Scanner(s);  // now using Sweden Locale          
       // input.useLocale(Locale.US);
       System.out.println(input.locale() + " " + input.nextDouble());

without the useLocale it throws an InputMismatch, with it it runs correctly.

The question is: why didn't OP see that Exception?

Edited by JamesCherrill

0

Ok that might be the problem. I got:

run:
Decimal separator for your locale: ,
BUILD SUCCESSFUL (total time: 1 second)

0

@JamesCherrill: "...why didn't OP see that Exception?"

I ran the code in NB, and it didn't throw an exception. It appears that while(input.hasNextDouble()) doesn't recognize the number as a double and just skips it.

Run the original code and mix numbers using "," (comma) as a decimal separator and numbers using "." (decimal) as the decimal separator in "orange.csv"

68.1
22.3
33,64

Then use the following:

Scanner input;

input  =  new Scanner(new FileInputStream ("C:\\javamapp\\orange.csv"));

while(input.hasNextDouble())  {                    

    double vikt = input.nextDouble();                                
    weight.add(vikt);
    System.out.println("read input: " + vikt);
}//while

Depending on what the decimal separator is set to, it will skip numbers that it doesn't see as doubles. If the decimal separator is set to "," (comma) it will skip numbers in the file with "." (decimal) as the decimal separator. If the decimal separator is set to "." (decimal), it will skip numbers in the file containing a "," (comma) as a decimal separator.

Edited by cgeier

0

You guys i love you! you solved my problem!!

run:
Filen har läst in till ArrayList weigth
Objektet har skrivits i orangeWeights.ser
62.91
57.29
55.32
60.62
62.35
66.33
63.38
54.57
74.22
64.88
59.09
55.41
52.95
66.05
62.19
60.38
66.47
58.13
50.03
55.01
62.83
45.71
52.61
64.99
58.44
51.17
63.42
67.44
64.18
65.01
68.01
43.46
52.12
36.94
45.32
56.01
56.77
57.17
68.95
59.67
55.8
73.29
62.86
56.89
45.82
59.25
65.2
57.84
58.88
61.54
63.21
57.91
59.66
52.17
47.03
48.98
56.12
68.3
58.39
69.77
65.91
63.67
55.8
62.3
63.34
64.46
62.6
69.77
64.17
55.94
95.42
91.99
98.17
98.84
95.04
99.68
96.59
99.61
90.99
88.14
92.01
92.59
90.57
95.06
91.73
74.86
55.76
92.77
66.62
70.4
87.29
86.97
77.83
90.78
73.25
73.62
70.05
83.76
81.2
85.94
92.73
77.26
96.22
86.79
86.87
85.27
83.26
79.24
79.81
73.57
88.0
56.98
85.71
72.15
71.53
78.89
85.11
64.37
64.04
98.54
64.75
83.94
79.54
88.49
84.41
87.2
75.92
71.98
84.51
82.27
87.99
66.28
79.02
74.19
84.05
92.41
82.4
74.63
94.15
84.55
69.55
79.97
87.03
89.62
89.19
89.47
67.79
74.36
78.51
77.84
72.09
72.94
105.49
35.99
54.92
76.31
77.14
62.71
70.61
39.44
51.38
71.46
67.73
68.57
76.98
81.67
77.79
90.02
67.08
63.5
88.26
62.65
48.17
55.92
62.54
54.4
67.84
43.43
74.33
101.3
68.15
98.92
63.17
52.27
65.84
86.06
40.35
73.73
86.88
101.12
78.42
61.13
59.74
97.93
73.18
62.11
74.81
70.1
71.54
68.79
orange
Medelvärdet är 71.35725000000001
BUILD SUCCESSFUL (total time: 2 seconds)

0

Thanks again! I learned so much thanks to you. I love forums like this, we learn a lot from eachother.

0

Cgeier,

Im greek :-) born and raised in cold Sweden, so you are right, no swedish looks here hehe

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.