This is a draft cheat sheet. It is a work in progress and is not finished yet.
Datatypes
public class Opdracht1 {
public static void main(String [] args) {
long a = 196587543L;
float b = 1.0065f;
boolean c = false;
boolean e = false;
double d = 0.0d;
double f = 0.0d;
}
}
|
For-loop
public class Opdracht3 {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
System.out.println(i);
}
}
}
|
While-loop
public class Opdracht4 {
public static void main(String[] args) {
int i = 0;
while (i <= 20) {
System.out.println(i);
i++;
}
}
}
|
Ternary
public class Opdracht6 {
public static void main(String[] args) {
for (int x = 0; x <= 20; x++) {
String result = (x % 3) == 0 ? "het getal is deelbaar door 3!" : "";
System.out.println(x + " " + result);
}
}
}
|
String omzetten naar...
String -> integer |
Integer.parseInt(str)
|
Integer -> String |
|
String -> Double |
Double.parseDouble(str)
|
Double -> String |
|
String methoden
str1==str2 //compares address;
String newStr = str1.equals(str2); //compares the values
String newStr = str1.equalsIgnoreCase() //compares the values ignoring the case
newStr = str1.length() //calculates length
newStr = str1.charAt(i) //extract i'th character
newStr = str1.toUpperCase() //returns string in ALL CAPS
newStr = str1.toLowerCase() //returns string in ALL LOWERCASE
newStr = str1.replace(oldVal, newVal) //search and replace
newStr = str1.trim() //trims surrounding whitespace
newStr = str1.contains("value"); //check for the values
newStr = str1.toCharArray(); // convert String to character type array
newStr = str1.IsEmpty(); //Check for empty String
newStr = str1.endsWith(); //Checks if string ends with the given suffix
|
|
|
Input gebruiker
import java.util.Scanner;
public class Opdracht2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String invoer = input.next();
System.out.println("Je hebt de volgende input gegeven: " + invoer);
}
}
|
naam.nextInt() -> Integer invoer
naam.nextDouble() -> Double invoer
naam.next() -> String invoer, zonder spaties
naam.nextLine() -> Alles voor de 'enter'
Getal deelbaar door (3)
public class Opdracht5 {
public static void main(String[] args) {
for (int x = 1; x <= 10; x++) {
if ((x % 3) == 0) {
System.out.println(x+ " " + "het getal is deelbaar door 3!" );
} else {
System.out.println(x);
}
}
}
}
|
Boolean String
equals |
naam.equals(" ") |
equalsignoreCase |
naam.equalsIgnoreCase(" ") |
startWith |
naam.startsWith(" ") |
endsWith |
naam.endsWith(" ") |
contains |
naam.contains(" ") |
Spaties aan eind van string weghalen
int len = str.length();
for( ; len > 0; len--)
{
if( ! Character.isWhitespace( str.charAt( len - 1)))
break;
}
return str.substring( 0, len);
|
|