#please help, my double is not read as a double from my text file into my array

1 messages · Page 1 of 1 (latest)

mystic isle
#
    
    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
empty ingotBOT
#

This post has been reserved for your question.

Hey @mystic isle! Please use /close or the Close Post button 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.

pliant kite
#
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

mystic isle
#

im using netbeans

pliant kite
#

it should have a reformat option too, but i cant say for sure

pliant kite
empty ingotBOT
#

💤 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.

mystic isle
#

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

stuck dirge
empty ingotBOT
#

💤 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.

mystic isle
pliant kite
#

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

mystic isle
#

how are you people helpers

#

fokol social skills