12. [15 pts] Write a Java program that prompts the user for an input string and
ID: 3761313 • Letter: 1
Question
12. [15 pts] Write a Java program that prompts the user for an input string and an integer. 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. Consider the following 2 Note that in example 2, the last 2 characters ( le) are not printed since they don't have 6 characters.Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author CheggExpert
*/
import java.util.Scanner;
public class Split {
public static void splitter(String inputStr,int inputInt)
{
String str = "";
int len = inputStr.length();
if(len>=inputInt)
{
for(int i=0;i<len;i++)
{
str = str+inputStr.charAt(i);
if(str.length()==inputInt)
{
System.out.println(str);
str = "";
}
}
}
}
public static void main(String[] args) {
// TODO code application logic here
// your code goes here
Scanner input = new Scanner(System.in);
System.out.println("Input a string : ");
String inputStr = input.nextLine();
System.out.println("Input an integer : ");
int inputInt = input.nextInt();
splitter(inputStr,inputInt);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.