- Introduction
- Super Simple Formula
- Your First Program
- Variables, Methods and Parameters
- Variables in Programming and Algebra
-
Math and Comparison Operators - If Statement
- While and For Loops
- Loopy Thinking
-
Data Types - Booleans
- Logical Operators (and Booleans)
- Output and Printing
- Printing and Loops
-
Arrays - Arrays and Loops
- Array Loop Practice
-
What's Next? - Quick Reference
Quick Reference
Quickly lookup Java basics, and load code examples directly into editor.
Method Basics
The header of the method declares what the method will return (such as an
Syntax note: Every line of code in Java needs to end with a
int
), the method name (doStuff
), and the method's parameters (such as a
and b
). The method body follows, where you usually need to write code that returns a value at the end.
//method header: methodName(parameters)
static int doStuff(int a, int b){
//your code here - erase this comment and write your code!
return 1;
}
Syntax note: Every line of code in Java needs to end with a
;
or it will be considered part of the next line. Comments are marked with 2 backslashes //
and are ignored by the computer.
Variables and Data types
Operations Booleans and Logic
Math operations are performed in the standard order. Note that division of integers will round down to nearest integer. The mod
%
operator returns the remainder after division.
int product = 4 * (1+2) / 2; //6.
int roughDivision = 8 / 3; //2
int remainder = 3 % 2; //1, the remainder of 3/2
booleans are used to store true/false values, such as the results of comparisons:
boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5); //equals
check = (five != 3); //not equals
check = (five >= 5); //greater or equals
The logical operator AND &&
will return true if multiple conditions are all true, and OR ||
will return true if at least one condition is true.
//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR
if else and while
The if statement will execute code only if a condition is true:
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
if(condition){
//doSomething
}
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
public static void controlFlow(int number) {
//this if/else block will print out 1 statement for any number
if(number > 10){
System.out.println("big number");
}
else if(number > 5){
System.out.println("medium number");
}
else{
System.out.println("small number");
}
//this loop will print the numbers from 0 to 9
int i = 0;
while(i < 10){
System.out.println(i);
i = i + 1;
}
}
Printing and Arrays
Print a newline
The above prints text and add a newline afterwards.
This code creates an empty 5-cell array:
This shortcut creates an array with numbers 0 to 4:
You can use for-loop to print values of array:
System.out.println("");
The above prints text and add a newline afterwards.
System.out.print
prints without a newline, but you can add in spaces " " to separate text.
System.out.println("hello"); //prints with newline
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world"
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets []
. This code creates an empty 5-cell array:
int[] ar1 = new int[5];
This shortcut creates an array with numbers 0 to 4:
int[] nums = {0,1,2,3,4};
You can use for-loop to print values of array:
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Array Loop Practice
Collapse Content
Show Content
The last node discussed the code to find the largest number in an array. Now see if you can modify it slightly to find the smallest number in an array.
More Practice
After you solve this challenge, see if you can solve Harder Array Loop Practice. You can also try solve some of the following Array Loop challenges:
Challenge
You will be given ar
, an array of numbers as input. Find and print the smallest number in ar
.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
niveoserenity
Sep 30, 10:45 PMThere is probably better ways but I worked out the largest number first.
Edison Lao
Oct 4, 2:12 PMstatic void doStuff(int[] ar){
//your code here
int smallest=0;
for(int i=0;i<ar.length;i++){
if(ar[i]<smallest){
smallest=ar[i];
}
}
return System.out.print(smallest);
Fábio Teles
Oct 16, 6:09 AMHello! Don't understand the error...any help would be apreciated!
mohammad wasi
Dec 14, 3:01 AMCan you help me understand what wrong I have done:
static void doStuff(int[] ar){
//your code here
int smallest=ar[0];
for(int i=0;i<ar.length;i++){
if(ar[i] < smallest){
smallest =ar[i];
}
Learneroo
Dec 14, 7:32 PM@mohammad, it looks like your print statement is outside the method, which doesn't work in Java. move it up to before the
}
so it runs when the for-loop is finsihed.mohammad wasi
Dec 15, 11:55 AMI did as you said... the code work in eclipse but not in above editor...
Learneroo
Dec 15, 1:22 PM@mohammad, it looks like there are some other errors in your code also. See if you can modify the code from the previous page to find the smallest number instead of the largest.
Carlos Díaz Ramirez
Jan 3, 10:58 PMi cant understand what its wrong here, i plug the code of the last page and modify and the output still doesnt make any sense.
'static void doStuff(int[] ar){
System.out.print(min);
}'
Peter Lacres
Jan 7, 9:55 AMi found the answer. There is a mistake in the boilerplate code, under 'doStuff(ar);' you have to put the line 'System.out.println("");'. Then the code should work.
//boilerplate code
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Learneroo
Jan 7, 10:05 AM@Peter, thanks. I mentioned in the question to print each number on its own line, so that if you use
System.out.println
(with theln
for line) it works, and you don't need to edit the boilerplatecode. However since this may be confusing, I updated the boilerplate code so both
print
andprintln
now work.Cliff Karlsson
Feb 17, 7:19 AMint smallest = ar[0];
for(int i=0; i < ar.length; i = i +1){
David
Feb 27, 11:43 AMSmall suggestion (however this may only be me), after reading the instructions it says "(Print each number on its own line.)", i think this is misleading or requires further clarification.
Because first it is said to print the smallest number but then print each number...
isnard
Mar 7, 11:47 AMint valor = ar[0];
for(int i = 0; i<= ar.length - 1; i = i + 1){
if (valor <= ar[i]){
}
else {
valor = ar[i];
}
}
System.out.print(valor);
//ar.length-1 because for example i have an string with 10 positions, but it always starts from 0 to 9 so them the value of the tenth it´s on the position 9. xD
// if (valor <= ar[i]) if the value stored in the variable valor is smaller than the value in the string you do nothing.
// else valor = ar[i]; it will store the new value only if it is bigger.
// System.out.print(valor); it will print only after it has finished all the for loop, out of the "{ }" of the loop
Ty B
Apr 6, 9:22 AMHello, I keep getting the correct outputs, but with .0 on the end. What am I doing wrong?
import java.util.*;
class Main{
static void doStuff(int[] ar){
for (int i = 0; i < ar.length; i++)
{
if (ar[i] < ar[0])
{
minimum = ar[i];
}
System.out.println( minimum );
}
}
Learneroo
Apr 6, 11:02 AM@TyB, use ints instead of doubles.
Ty B
Apr 6, 6:17 PMThank you!
許友誠
May 5, 11:59 AMMichał Targiel
May 28, 2:15 PM' static void doStuff(int[] ar){
int small=ar[0];
for (int i=0; iar[i]){
small=ar[i];
}
}
System.out.print(small);
}'
Olga
Jul 21, 4:36 PMI used this strategy: static void doStuff(int[] ar){
int smallest = 0;
int teller = 0;
for(int i = 0; i < ar.length; i = i+1){
if(teller == 0){
paul fothergill
Jul 26, 2:56 PMim getting errors like
Main.java:12: error: ';' expected
public static void main(String[] args) {
John
Aug 3, 3:21 PMwhat does this mean?
cannot return a value from method whose result type is void
return smallest;
flowra
Feb 13, 5:40 PManother solution using for each:
int tmp = ar[0];
for (int element : ar)
{
if (element < tmp ) tmp = element;
}
Bernard Mitchell
May 30, 8:41 PMugghh had to cheat on this one ....will do better next time