University College of Applied SciencesProgramming 1
العربية

Reference

Everything you reach for while coding, in one place.

The skeleton of every program

import java.util.Scanner;   // only when reading input

public class Main {
    public static void main(String[] args) {
        // your statements go here
    }
}

NetBeans shortcuts

ShortcutWhat it does
Ctrl+Shift+NNew project
sout + TabInserts a print statement
psvm + TabInserts the main method
F6Run the project
Shift+F6Run the open file
Ctrl+SSave the file
Alt+Shift+FFormat the code
Ctrl+/Comment or uncomment the line
Ctrl+F5Start debugging
F8Next line while debugging
F7Step into a method while debugging

Types

TypeExampleprintf specifierReading it with Scanner
int19%dnextInt()
long10L%dnextLong()
double3.75%fnextDouble()
booleantrue%bnextBoolean()
char'A'%cnext().charAt(0)
String"Sara"%snext() / nextLine()

printf specifiers

SpecifierOutputMeaning
%d25Whole number
%f3.140000Decimal with six places
%.2f3.14Two decimal places
%sAliText
%cACharacter
%btrueBoolean
%5d 25Width 5, right-aligned
%-5d25 Width 5, left-aligned
%05d00025Padded with zeros
%n↵New line
%%%A literal percent sign

Operator precedence

PriorityOperators
1 (highest)() ++ -- ! (cast)
2* / %
3+ -
4< <= > >=
5== !=
6&&
7||
8 (lowest)= += -= *= /= %=

Glossary

TermIn ArabicMeaning
variableمتغيرA name holding a value that can change
data typeنوع البياناتDecides which values a variable accepts
declarationتصريحReserving a variable with a type and a name
assignmentإسنادPutting a value in a variable
expressionتعبيرCode that computes to a value
statementأمرA complete instruction, usually ending in a semicolon
operatorعاملA symbol that performs an operation, such as +
conditionشرطAn expression that is true or false
loopحلقةRepeating a group of statements
methodدالةA named group of statements doing one task
parameterمعاملA variable through which a method receives a value
argumentوسيطThe value passed in a call
returnإرجاعGiving a value back to the caller
arrayمصفوفةA group of values of one type at numbered positions
indexموقع (فهرس)An element's position, starting at 0
classصنفThe unit our code is written in
packageحزمةA folder grouping classes
compilerالمترجمTurns code into a runnable form
bytecodeالبايت كودThe form the Java Virtual Machine runs
exceptionاستثناءAn error that happens while running
debuggingتتبّع الأخطاءRunning a program step by step to understand and fix it

Error message guide

MessageWhat it meansHow to fix it
';' expectedJava needs a symbol here that is missing, most often the ; at the end of a statement.Check the end of the line just before the mark and add the missing symbol.
cannot find symbolYou used a name the compiler does not know: it was never declared, or it is spelled differently.Declare the variable before using it, and match upper and lower case exactly.
cannot find symbol (method)You called a method that does not exist with this name or these arguments.Check the method name and its arguments, for example println, not printn.
cannot find symbol (class)This class is unknown; usually an import line is missing.Add the import, for example import java.util.Scanner;.
incompatible typesThe value does not fit the variable, such as text in an int or a double in an int.Change the variable type or convert the value, such as (int) 3.5 or Integer.parseInt("19").
not a statementThis expression does nothing on its own, such as x + 1;.Store or print the result, such as x = x + 1;.
unclosed string literalA " was opened and not closed on the same line.Add the missing quotation mark.
unclosed commentA /* comment was never closed, so the rest of the file became a comment.Add */ at the end of the comment.
reached end of file while parsingThe file ended before every brace closed, so a } is missing.Count the braces. In NetBeans, Alt+Shift+F formats the code and makes this visible.
illegal start of expressionSomething here cannot begin an expression, often because of a missing brace before it.Check the braces and the line before the mark.
illegal start of typeCode is somewhere it is not allowed, often outside main or after an extra brace.Keep your statements inside the braces of main.
variable x might not have been initializedYou read a variable that may not hold a value yet.Give it a value when you declare it, such as int x = 0;.
variable x is already definedThe same name is declared twice in the same scope.To assign instead, drop the type on the second line: x = 5;.
missing return statementThe method can finish without returning a value, although it is not void.Make sure every path through the method ends in a return.
incomparable typesThese two types cannot be compared.Convert one value, or compare strings with equals.
bad operand types for binary operatorThis operator does not work on these types, such as subtracting text from a number.Check the types on both sides of the operator.
'else' without 'if'An else has no if right before it, often because of a ; after the condition or a missing brace.Remove any ; after if (...) and check the braces.
break outside switch or loopbreak only works inside a loop or a switch.Move it into the loop, or use return to leave the method.
unreachable statementExecution can never reach this line, such as a line after return or break.Delete the line or move it before the statement that ends execution.
method cannot be applied to given typesThe arguments in this call do not match the method definition.Match the number and types of arguments to the definition.
class X is public, should be declared in a file named X.javaA public class name must match its file name exactly.Rename the class or the file so that they match.
integer number too largeThis number is larger than an int can hold.Use long and add L, such as 3000000000L.
incompatible typesThe type of this value does not fit what is needed here.Check the type of the value and of the variable.
ArithmeticException: / by zeroAn integer was divided by zero while the program ran.Check that the divisor is not zero before dividing.
ArrayIndexOutOfBoundsExceptionThis index is outside the array. Indexes run from 0 to length - 1.Check the loop condition: use i < a.length, not i <= a.length.
StringIndexOutOfBoundsExceptionThis character position is outside the string.Positions run from 0 to length() - 1.
NullPointerExceptionYou used a variable holding null, which refers to nothing.Create the object or assign a value before using it.
NumberFormatExceptionYou tried to convert text to a number, but the text is not a valid number.Check the text before calling Integer.parseInt.
InputMismatchExceptionScanner asked for a number but the input was text.Enter the right type, or check with hasNextInt().
NoSuchElementExceptionThe program asked for more input than was provided.Match the number of reads to the input provided.
IllegalFormatConversionException: d != java.lang.StringThe format specifier does not fit the value, such as %d with text or a decimal.Use %d for whole numbers, %f for decimals and %s for text.
MissingFormatArgumentException: Format specifier '%d'The template has more specifiers than values.Give one value per specifier, in order.
StackOverflowErrorA method kept calling itself until the call stack filled up.Add a stopping condition to the recursive method.