nextInt()

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

int

nextInt()

The nextInt() method of a Scanner object reads in a string of digits (characters) and converts them into an int type. The Scanner object reads the characters one by one until it has collected those that are used for one integer. Then it converts them into a 32-bit numeric value. Usually that value is stored in an int variable.

character conversion

The picture shows a program that reads in character data and then converts it to an integer which is stored in num. Next the program does arithmetic with num and stores the result in square. Finally the result is sent to println which converts the numeric result into characters and prints them out.

The nextInt() method scans through the input stream character by character, gathering characters into groups that can be converted into numeric data. It ignores any spaces and end-of-lines that may separate these groups.

QUESTION 12:

Can arithmetic be done with strings of characters?