Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active November 15, 2019 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/a410d4d48cf088d4e196e60f6825bb26 to your computer and use it in GitHub Desktop.
Save bytecodeman/a410d4d48cf088d4e196e60f6825bb26 to your computer and use it in GitHub Desktop.
Read Data from a file or website
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadData {
public static void main(String args[]) throws FileNotFoundException {
File file = new File("data-750-10-6.txt");
Scanner sc = new Scanner(file);
int count = sc.nextInt();
for (int i = 0; i < count; i++) {
double number = sc.nextDouble();
System.out.printf("%10.3f\n", number);
}
sc.close();
}
}
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class ReadDataFromURL {
public static void main(String args[]) throws IOException {
URL url = new URL("https://cs.stcc.edu/~silvestri/csc111/data-750-10-6.txt");
Scanner sc = new Scanner(url.openStream());
int count = sc.nextInt();
for (int i = 0; i < count; i++) {
double number = sc.nextDouble();
System.out.printf("%10.3f\n", number);
}
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment