- File | New | Project from Version Control... (or Get from VCS at the Welcome window).
- Enter
https://github.com/ProfessorStrenn/JavaCoreTemplatefor the URL. - In the Directory textbox, change the last directory element from
JavaCoreTemplateto the name of your project - e.g.MMarciano_RockCountdown_F35. Click Clone.
The template's src/cs106 folder includes a Main.java class that is ready to go. It has a bunch of functionality
statically imported, allowing you to do things like:
Eliminate the EEEVIL System.out. and just use:
println("Hello");Read a line of text from the console:
String s = readLine();Read a whole file into a String with a single line of code:
String quakeText = readFile("4.5_week.atom");Read a file into a list of Strings:
List<String> words = readFileAsLines("dict.txt");Write a string to a file:
writeFile("file.txt", str);Write a list of strings to a file:
writeFileAsLines("lines.txt", list);printf w/o requiring System.out. in front:
printf("%d words in %s\n", words.size(), path);
print("Hello");Parse ints, floats, etc w/o having to put Integer. in front:
int age = parseInt(text);Easily create mutable lists and maps.
var list = listOf("This", "That", "The other");
var map = mapOf("key1", value1, "key2", value2);Use range() to loop in a pythonic manner:
for (var x : range(10))
println("Hello " + x);The JavaCoreTemplate includes the
exceptional StringUtils
, which has join, substringsBetween, reverse, etc. For example:
String[] titles = substringsBetween(quakesXml, "<title>", "</title>");Use IOUtils.toString() to easily read from an URL into a String, and FileUtils.copyUrlToFile() to read from an URL
into a file.
// Get the weather in Santa Barbara
var url="https://api.open-meteo.com/v1/forecast?latitude=34.41&longitude=-119.71¤t_weather=true&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch&timezone=America%2FLos_Angeles";
var response=IOUtils.toString(new URL(url),StandardCharsets.UTF_8);PMD support, including a tailored set of rules (cs106.ruleset).
JavaCoreTemplate's Main class already has the useful functions statically imported. To add this functionality to other
classes, simply put the following static imports at the top of the .java file:
import static sbcc.Core.*;
import static org.apache.commons.lang3.StringUtils.*;
import static java.lang.System.*;
import static java.lang.Math.*;