I am trying to write a Java program that:
Store the string in a variable called inputStr and the integer in a variable called inputInt.
Pass inputStr and inputInt to a method called splitter. The splitter method splits the input
string by exactly inputInt characters at a time and prints each in a line. Only the splits that
have exactly inputInt characters should be printed.
This is the code I have so far:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("What is your Phrase? ");
String inputStr;
inputStr = keyboard.nextLine();
System.out.println("Enter a Integer");
int inputInt;
inputInt=keyboard.nextInt();
for(int i =1;i<20;i++){
inputStr.substring(0, inputInt);
}
The program should look like this:
Please enter a string: ThisIsAnExample
Please enter an integer: 3
Thi
sIs
AnE
xam
ple
Example2:
Please enter a string: ThisIsAnotherExample
Please enter an integer: 6
ThisIs
Anothe
rExamp
Note that in example 2, the last 2 characters (“le”) are not printed since they don’t have 6 characters.