Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHind Nabet
404 PointsParseInt, is used to convert a String into an int. Could someone explain this --> num = Integer.parseInt(args[0]);
I just can not see where the String is being converted into an int. Isn't it an integer from the beginning of the code?
public class TowersOfHanoi {
// print out instructions for moving n discs to
// the left (if left is true) or right (if left is false)
public static void moves(int n, boolean left) {
if (n == 0) return;
moves(n-1, !left);
if (left) StdOut.println(n + " left");
else StdOut.println(n + " right");
moves(n-1, !left);
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
moves(n, true);
}
}
1 Answer
Seth Kroger
56,413 PointsIf you look at the line above, args
is declared as an array of Strings. What the code is doing is taking the first word in the command line after java TowersOfHanoi
and tries to convert it into a number. It uses a class method (i.e., a static method) from the built-in Integer class to do it.