Question

Java code to read from .csv file i currently have a .csv file that looks like...

Java code to read from .csv file

i currently have a .csv file that looks like this:

39.743222, -105.006241, Hospital
39.743981, -105.020017, Home
39.739377, -104.984774, Firehouse
39.627779, -104.839291, McDonald's
39.731919, -104.961814, Chipotle

I need to write code to extract the doubles from each line to use for my Linked List.

this is what i have so far:

Scanner in = new Scanner(new FileInputStream(DATA_FILE));
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your code for extracting float values is here; Since you hadn't asked to append to the linked list, I assume you have figured it out.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.*;

public class CSVReader {

public static void main(String[] args) {
//Enter your full csv destination path
String csvFile = "/Your/csv/file/destination/path/file.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";

try {

br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {

// use comma as separator
String[] file = line.split(cvsSplitBy);
  
//Regex pattern to extract float values
String regex="([0-9]+[.][0-9]+)";
  
for(int i = 0;i<file.length;i++){
String input= file[i];
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(input);
while(matcher.find()){
float a = Float.parseFloat(matcher.group());
// Now your float variable 'a' has been extracted; use it to append in your linked list
//Your code here.
}
}

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}

Screen shot of the code:

If you face any problem regarding the current code, or if you want help implementing a linked list, please ask in the comments section, I'll be pleased to help.

If you did like my answer, please do give a thumbs up!!!

Add a comment
Know the answer?
Add Answer to:
Java code to read from .csv file i currently have a .csv file that looks like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • Write code to read student data from a csv file and add (prepend) that into a...

    Write code to read student data from a csv file and add (prepend) that into a linked list. Assume the code is written in only one file - main.c. You are REQUIRED to write the struct definition for the linked list node. . The input file name is taken from command line argument. . You can write everything except the struct definition in the main function, or can create multiple functions up to you. Following is the sample file data....

  • Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, ...

    Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, nextInt, next, next, and nextLine. Then I have to instantiate a User object using those 5 fields and insert that object in the usersArr. I just need to see what this would look like if a text file with 5 fields for 5 users existed. Here's my code so far: int [] usersArr = new int[200];           System.out.print("Enter file name:...

  • Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create...

    Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create a graph using the input data. The input file must have the following format: 6 049 0 27 2 3 1 25 2 1 5 6 352 4 5 1 Here, the number on the first line represents the number of vertices in the graph. The letter on the second line indicates whether this is a directed graph ('D') or an undirected graph ('U')....

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • Hi, i am working with MATLAB. I have a text file from which I have to...

    Hi, i am working with MATLAB. I have a text file from which I have to read in my data. So far i have this in my code. fid = fopen('ecgnormaloff.txt', 'r'); data = textscan(fid,'%f') According to all the websites, this should be enough to read in my points, however, I get this as my output data = 1×1 cell array {0×1 double} >> And when I try to access my data, there is nothing. I do not want to...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • I am told to create a fillArray method that will read integers from the file "data.txt"...

    I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it. This is what I have so far: public void fillArray() { int curVal;    Scanner input = null; try { input = new Scanner(new File("data.txt")); // set the current number of items in the array to zero while (input.hasNextInt()) { curVal = input.nextInt();...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT