Simply using e.printStackTrace()
is usually not advised, and this SO question SO question is a good read on this subject. Does your code really not need to handle StringIndexOutOfBoundsException
in a more graceful manner?
Simply using e.printStackTrace()
is usually not advised, and this SO question is a good read on this subject. Does your code really not need to handle StringIndexOutOfBoundsException
in a more graceful manner?
Simply using e.printStackTrace()
is usually not advised, and this SO question is a good read on this subject. Does your code really not need to handle StringIndexOutOfBoundsException
in a more graceful manner?
Variables type declaration
private static ArrayList<String> pathPermutations = new ArrayList<String>();
private static ArrayList<String> acceptedPathPermutations = new ArrayList<String>();
It is usually recommended to declare variables by the interface they implement, unless implementation-specific methods are required. Since these two are used simply as List
s, they should be declared as such:
// Assuming Java 7
private static List<String> pathPermutations = new ArrayList<>();
private static List<String> acceptedPathPermutations = new ArrayList<>();
try-with-resources
If you are on Java 7, you can use try-with-resources
on the Scanner
object for efficient handling of the underlying I/O resource.
try (Scanner myScanner = new Scanner(System.in)) {
// ...
}
Getting inputs
numToEnter = myScanner.nextInt();
mRankingDesired = myScanner.nextInt();
//subtract one from the desired ranking to align rankings up with actual...
mRankingDesired--;
numToEnter--;
You should just perform the subtraction together with the assignment as such:
// Subtract from input to align rankings with actual list indices.
numToEnter = myScanner.nextInt() - 1;
mRankingDesired = myScanner.nextInt() - 1;
Magic method
//Calculate all permutations of possible "run" paths.
calculatePermutations(numToEnter);
It is not clear how calling the method above also magically initializes acceptedPathPermutations
correctly to display the results. Looking at how your method calls are chained, you may want to consider explicitly getting all the permutations in the main()
method, then filter them to get your acceptedPathPermutations
values:
List<String> pathPermutations = calculatePermutations(numToEnter);
List<String> acceptedPathPermutations = removeInvalidPaths(pathPermutations);
That means abolishing the removeInvalidPaths()
call below:
public static void appendCharacters() {
for (int i = 0; i < pathPermutations.size(); i++) {
String replacement = "E" + pathPermutations.get(i) + "S";
pathPermutations.set(i, replacement);
System.out.println(pathPermutations.get(i));
}
// not required
// removeInvalidPaths();
}
Loop iteration
When you create a for
-loop where the iteration value i
is used solely for the List.get(int)
call, you can use the enhanced for-each
loop to achieve the same with lesser code. For example:
public static List<String> removeInvalidPaths(List<String> pathPermutations) {
List<String> results = new ArrayList<>();
for (String path : pathPermutations) {
// consider renaming 'checkPathValidity' as 'isPathValid'
if (isPathValid(path)) {
results.add(path);
}
}
// don't think you need the following, they should be done outside the method
// pathPermutations.clear();
// printOutAcceptedPaths();
return results;
}
Java 8
If you happen to be on Java 8, you may want to consider replacing some of the explicit for
-loops with Stream
-based processing. Using the same removeInvalidPaths
as an example:
public static List<String> removeInvalidPaths(List<String> pathPermutations) {
return pathPermutations.stream().filter(Main::isPathValid)
.collect(Collectors.toList());
}
This uses the (renamed) isPathValid
method reference to filter on the Stream
of pathPermutations
elements, and then return
-ing the collected toList()
results to the caller.
Catching Exception
s
Simply using e.printStackTrace()
is usually not advised, and this SO question is a good read on this subject. Does your code really not need to handle StringIndexOutOfBoundsException
in a more graceful manner?