Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

Im trying ro read a csv file and store it into a 2d array but im getting an error when I run my java code. my csv file contains 69 lines of data

Below is my code:

import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;

public class CompLab2 {
    public static String [][] getEarthquakeDatabase (String Filename) {     //will read the csv file and convert it to a string 2-d array
        String [][] Fileinfo = new String [69][22];
        int counter = 0;
        File file = new File(Filename);
        try {
            Scanner scnr = new Scanner(file);
            scnr.nextLine(); //skips the label in the first row of the file
            while (scnr.hasNextLine()) {  // this while loop will count the number of values in the usgs file
                counter += 1; // increases by one each time a line is read
                scnr.nextLine();
            }
            while (scnr.hasNext()) {  // reads through csv file while there is a line of data
                String data = scnr.nextLine();                            
                String[] values = data.split(","); // splits the data at the commas
                double closingValue = Double.parseDouble(values[4]);
                System.out.println(values[4]); // prints the data at element 5
            }
            scnr.close(); // closes the scanner
        } catch (FileNotFoundException e) { //throws exception if file is not found
            e.printStackTrace();
        }
        return Fileinfo;
    }
           
    public static void main(String[] args) {
        String nameOfFile = "query(2).csv";  //stores the csv file in a string    
        String [][] earthquakeDatabase = getEarthquakeDatabase (nameOfFile); // creates 2d string array thats equal to method getEarthquakeDatabase
        System.out.println("Expected number of rows: 69");
        System.out.println("Number of rows from the array: " +earthquakeDatabase);

        double [] earthquakeValues = getEarthquakeValues(earthquakeDatabase);
    }
}
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY