Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created October 18, 2019 11:38
Show Gist options
  • Save bytecodeman/ef4e0d2ab9a9beab177ee6426eb213ec to your computer and use it in GitHub Desktop.
Save bytecodeman/ef4e0d2ab9a9beab177ee6426eb213ec to your computer and use it in GitHub Desktop.
Read Names From a CSV comma delimited file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadNames {
public static void main(String args[]) throws FileNotFoundException {
Scanner sc = new Scanner(new File("yob2015.txt"));
sc.useDelimiter("\\s*,\\s*|\\s+");
while (sc.hasNextLine()) {
String name = sc.next();
String sex = sc.next();
int number = sc.nextInt();
sc.nextLine();
System.out.printf("%15s%2s%10d\n", name, sex, number);
}
sc.close();
}
}
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class ReadNamesFromURL {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(new URL("https://cs.stcc.edu/~silvestri/names/yob2015.txt").openStream());
sc.useDelimiter("\\s*,\\s*|\\s+");
while (sc.hasNextLine()) {
String name = sc.next();
String sex = sc.next();
int number = sc.nextInt();
sc.nextLine();
System.out.printf("%15s%2s%10d\n", name, sex, number);
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment