private double[] gradesArr = new double[50];
private int size = 0;
public GradeTracker(){
try {
Scanner scFile = new Scanner(new File("Grades.txt"));
while(scFile.hasNextDouble()){
gradesArr[size] = scFile.nextDouble();
size++;
}
} catch (FileNotFoundException ex) {
System.out.println("File not found");
}
}
@Override
public String toString() {
StringBuilder temp = new StringBuilder();
for (int i = 0; i < size; i++) {
temp.append(gradesArr[i]).append("\n");
}
return temp.toString();
}
}```
I tested it and the values are read into the array as non-double values but my text file looks like this: 78.5
89.2
92.0
65.7
73.8
81.4
70.1
84.6
77.3
88.9
#please help, my double is not read as a double from my text file into my array
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @mystic isle! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
public class GradeTracker {
private double[] gradesArr = new double[50];
private int size = 0;
public GradeTracker(){
try {
Scanner scFile = new Scanner(new File("Grades.txt"));
while(scFile.hasNextDouble()){
gradesArr[size] = scFile.nextDouble();
size++;
}
} catch (FileNotFoundException ex) {
System.out.println("File not found");
}
}
@Override
public String toString() {
StringBuilder temp = new StringBuilder();
for (int i = 0; i < size; i++) {
temp.append(gradesArr[i]).append("\n");
}
return temp.toString();
}
}
i hope u are using an IDE (intelliJ or Eclipse)
there is an option called reformat code or re-indent lines, etc
use that option
u tell what the values in the array are, but u dont say what the values r after being read in
also: u should use try-with-resources
try (Scanner scFile = new Scanner(new File("Grades.txt")) {
while(scFile.hasNextDouble()){
gradesArr[size] = scFile.nextDouble();
size++;
}
} catch (FileNotFoundException ex) {
System.out.println("File not found");
}
this will close the file, which btw is something ur not doing
im using netbeans
it should have a reformat option too, but i cant say for sure
u still need to show what the numbers look like before and after reading them from the file
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
i actually just fixed the issue by realising that nextDouble() looks for a comma in the text file, so i just said Double.parseDouble(scFile.nextLine()); lol
It doesn't "look for a comma". It takes the next number and expects that the fractional separator is a comma (by default in your locale.) That simply isn't in any way something that can be described as "not read as double"
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
well as someone who didnt know what was going on with nextDouble until I asked one of my professors, it looked like a not read as double for me when I was debugging the problem. And "look for a comma" is just the way i explained it so i didnt have to go write out an entire sentence about fractional seperators.
the complaint is not that u didnt know whats going on with nextDouble
the complaint is that u only gave us part of the information needed to help u
despite being asked more than once