Read Java and make your first edit
For: anyone new to Java. Before this: Set up, pages 1 to 7. At the end you can read a Java file, change one line safely, and make new files in TeamCode.
Reading Java without writing it
Section titled “Reading Java without writing it”You do not need to write Java yet. You need to read it well enough to change one line safely. This small
file is the FTC sample ConceptNullOp.java, shortened:
package org.firstinspires.ftc.robotcontroller.external.samples;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;import com.qualcomm.robotcore.eventloop.opmode.OpMode;import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
@TeleOp(name = "Concept: NullOp", group = "Concept")@Disabledpublic class ConceptNullOp extends OpMode {
@Override public void init() { telemetry.addData("Status", "Initialized"); // a line on the Driver Station } ...}What each part is:
- A file holds one class (one piece of a program). The file name and the class name are the
same:
ConceptNullOp.javaholdsclass ConceptNullOp. - The
packageline (the first line) says which folder the file is in, with dots in place of slashes. It must match the folder, or the file does not build. importlines name code from other places that this file uses (here, from the FTC SDK).@TeleOp(...)and@Disabledare notes for the Driver Station.@TeleOp(name = "...")is the name in the TeleOp list.@Disabledhides the OpMode from the list.{and}(curly brackets) hold a group of lines together. Every{has its own}further down. The class has one pair. Each part inside it has its own pair.;(semicolon) ends a statement (one command), like a period ends a sentence.//starts a comment: words for people, from//to the end of the line. Java ignores them."text in quotes"is text, used as it is (a name on the Driver Station, a motor name). Words without quotes are names in the code (telemetry,init). Do not add or remove quotes.- Capitals count.
Intake,intakeandINTAKEare three different names to Java.
Red text is an error. A name underlined in red, or a red line in the Build tab, is an error. Read only the first error: the later ones often come from the first. It looks like this:
Robot.java:12: error: ';' expectedThat says: the file (Robot.java), the line number (12), and the problem (a ; is missing). Click it
to go to the line. Fix it, then build again. Why the first: one missing ; or } can make
twenty more errors below it, and they all go away with the first fix.
Java words
Section titled “Java words”You meet these words in the code, and again in these docs.
- variable: a name that holds a value, like a box with a label.
maxPowerholds0.4. double: a kind of variable that holds a number with a decimal point (0.4,96).intholds a whole number (3).boolean: a kind of variable that holds onlytrueorfalse(yes or no).true/false: the two answers to a yes/no question, for example “is the button pressed?”.- method: a named list of commands, like a custom block.
init()andbuttons(...)are methods. The( )after the name hold its inputs. void: written before a method’s name when the method hands nothing back.return: hands a result back from a method, and ends the method.- object: one thing the program made from a class, with its own values: your robot, its intake.
robot.intake.in()asks the intake object to run itsinmethod. public: other files may use this name. (private: only this file may.)static: belongs to the class itself, not to one object, so there is only one. InTunables.java,public static double maxPoweris one number the whole program shares.
Copy a file into TeamCode and change one line
Section titled “Copy a file into TeamCode and change one line”Now run your own first OpMode: a copy of the FTC sample above. It needs no motors.
-
In the Project panel, find the sample:
FtcRobotController/src/main/java/org/firstinspires/ftc/robotcontroller/external/samples/ConceptNullOp.java. -
Click it, and copy it (Edit > Copy). Click the
teamcodefolder (Set up 3), and paste it (Edit > Paste). If Android Studio asks, click OK (keep the name). Why copy: never change FTC’s own files. Your team’s code lives only in TeamCode. -
Open the copy (in
teamcode). Change exactly one line, the first one:Before:
package org.firstinspires.ftc.robotcontroller.external.samples;After:
package org.firstinspires.ftc.teamcode;Why: the file is now in the
teamcodefolder, and thepackageline must match its folder. (Android Studio may change it for you when you paste. Check it anyway.) -
Delete the whole line
@Disabled. Why: with it, the Driver Station hides the OpMode. -
Build (hammer), then Run (triangle) to install it.
-
On the Driver Station, pick Concept: NullOp in the TeleOp list. Press INIT: it shows
Status: Initialized. Press ▶: it shows the run time. Then press STOP.
Common mistakes:
- Two
packagelines, or a first line with a typo: the build saysclass, interface, or enum expected. The file must have exactly onepackageline, at the top. - A deleted
;or}: the build says';' expectedorreached end of file while parsing. Press Edit > Undo until the file is as it was.
Make a new file or folder
Section titled “Make a new file or folder”Later pages say “make a file” or “make a folder”. Do it in the Project panel (the Project view, Set up 3). You make each kind a different way:
- A new Java file (a class, for example
HardwareCheck.java): right-click theteamcodefolder and click New > Java Class. Type the name without.java(HardwareCheck), pick Class, and press Enter. Android Studio writes thepackageline for you. Then replace everything in the new file with the whole file the page shows (select all with Edit > Select All, then paste). Check that the first line is stillpackage org.firstinspires.ftc.teamcode;. Why: the file name must be the class name, and thepackageline must match the folder. - A Java folder (a folder of code, for example
autos): right-click theteamcodefolder, click New > Package, and typeautos(after theteamcode.that is already there). A file in it starts withpackage org.firstinspires.ftc.teamcode.autos;. Why “Package”: Java calls a folder of code a package. - A plain text file (not Java: for example
local.propertiesor a scenariomy-test.sim): right-click the folder it goes in, click New > File, and type the full name with its ending (local.properties,my-test.sim). Do not add.txt. If Android Studio asks what kind of file it is (“Register New File Type Association”), pick Text. Why the full name: the ending tells the tools what the file is. The tools do not findmy-test.sim.txt. - A plain folder (not Java: for example
simulator/scenariosnext toTeamCode): right-click the top project folder (the one withTeamCodein it), click New > Directory, and typesimulator/scenarios. The/makes a folder inside a folder.
The example robots put the autonomous programs in a Java folder autos inside teamcode (kind 2).
- If you used Scratch or Blocks before, read From Blocks to Java: it shows where each block you know went.
- Pick your start: get the five robot files, then The five files to drive.
Something wrong on this page? Tell us with a short GitHub form.