Instantly share code, notes, and snippets.

Embed
What would you like to do?
BabyNames HW - How to read a comma delimited file
package babynames;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadBabyNames {
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();
}
}
package babynames;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class ReadBabyNamesFromURL {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(new URL("https://cs.stcc.edu/~silvestri/babynames/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