What is the difference between next() and nextLine()?
There are many types of energy, which can be classified into two main types: potential and kinetic: Potential energy: Stored energy, such as chemical, gravitational, mechanical, and nuclear energy Kinetic energy: Energy in motion, such as electrical, heat, light, and sound energy Here are some exampRead more
There are many types of energy, which can be classified into two main types: potential and kinetic:
Potential energy: Stored energy, such as chemical, gravitational, mechanical, and nuclear energy
Kinetic energy: Energy in motion, such as electrical, heat, light, and sound energy
Here are some examples of different types of energy:
Chemical energy
Energy stored in the bonds of atoms and molecules, such as in batteries, biomass, petroleum, natural gas, and coal
Mechanical energy
Energy stored in objects by tension, such as in compressed springs and stretched rubber bands
Nuclear energy
Energy stored in the nucleus of an atom, which holds the nucleus together
Gravitational energy
Energy stored in an object’s height, such as when a person rides a bicycle down a steep hill
Radiant energy
Electromagnetic energy that travels in transverse waves, such as visible light and infrared radiation


1. next() Function: Reads the next token (word) from input. Delimiter: Stops reading when it encounters whitespace (space, tab, or newline). Ignores: Leading whitespace before the token. Use case: Good for reading single words. Example: Scanner sc = new Scanner(System.in); System.out.print("Enter yoRead more
1.
next()Function: Reads the next token (word) from input.
Delimiter: Stops reading when it encounters whitespace (space, tab, or newline).
Ignores: Leading whitespace before the token.
Use case: Good for reading single words.
Example:
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.next();
System.out.println("You entered: " + name);
Input:
Rahul Sharma
Output:
You entered: Rahul
👉 It only captures
"Rahul"becausenext()stops at the first space.2.
nextLine()Function: Reads the entire line of input (until Enter/
\n).Delimiter: Stops only when the newline character is encountered.
Use case: Good for reading sentences or full lines with spaces.
Example:
Scanner sc = new Scanner(System.in);
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.println("You entered: " + name);
Input:
Rahul Sharma
Output:
You entered: Rahul Sharma
👉 Here it captures the whole line, including spaces.
⚡ Key Differences Table
next()nextLine()\n)