Write a program that reads an integer and a string from the input. If the integer is greater than 0, print the string that many times. If the integer is 0 or negative, print "Invalid input!" without throwing any exceptions.
NullPointerException
Print string multiple times based on input
Report a typo
Sample Input 1:
3
HelloSample Output 1:
Hello
Hello
HelloSample Input 2:
0
WorldSample Output 2:
Invalid input!Write a program in Java 25
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Read an integer from the input
String integerStr = reader.readLine();
int integer = Integer.parseInt(integerStr);
// Read a string from the input
String string = reader.readLine();
// Check if the integer is greater than 0
// If so, print the string that many times
// Otherwise, print "Invalid input!"
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.