-
-
Save bytecodeman/ef4e0d2ab9a9beab177ee6426eb213ec to your computer and use it in GitHub Desktop.
Read Names From a CSV comma delimited file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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