String is the most used class in Java.

Sharpen your knowledge with the simple String exercises you can find below.

Exercise 1

Write a Java program that will read numerical values from the standard input until a user enters ‘x’ or ‘X’. 

The program should sum up the entered values and print the sum to the console.

Exercise 2

Write a Java program that supports basic mathematical operations: addition, subtraction, multiplication, and division.

It should prompt the user to input two numerical values. After input, the program should offer the user which operation they want to perform (1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division).

Upon user selection, it should execute the chosen operation and print its result to the console.

Exercise 3

Write a Java program that will ask a user to enter a sentence. The program should find the total number of words used in the sentence and print the result to the console.

Exercise 4

Write a Java program that will read three numerical values from the console.

You need to calculate the average number of the entered numbers and print the result on the console.

Exercise 5

Write a Java program that will ask the user to enter a sentence.

After entering the sentence, the user inputs a numerical value representing the word number in the sentence.

The program prints the selected word to the console. If the word at the given number does not exist, it should print ‘Word does not exist’.

Exercise 6

Write a Java program that will ask a user to enter a numerical value.

If the number has less than two digits, it should return the message ‘Number is too small.‘ Otherwise, it should reverse the given value and print the result to the console.

Exercise 7

Write a Java program that will ask a user to enter a textual input.

From the provided input, all characters other than letters need to be removed and printed on the screen.

If the text does not contain any letters, it should print ‘Input doesn’t contain letters.’

Similarly, if the text contains only letters, it should print ‘Input contains only letters‘.

Send Us Your Solution

If you’re interested in our feedback, please send us your solution here. We’ll review it and let you know.

    By Ana Peterlić

    Ana is a Java Software Engineer with over 6 years of experience in designing and developing software solutions. She loves tutoring and helping others understand Java ecosystem.