0

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.

kyl walfer
  • 23
  • 2
  • 6
  • 1
    You forgot to ask a question :) Have you looked up how to take input and parse a string in Java? – tnw Apr 14 '15 at 15:36
  • I thought I did ask a question? – kyl walfer Apr 14 '15 at 15:36
  • What is parse string? – kyl walfer Apr 14 '15 at 15:38
  • 1
    `What is parse string?` You need to work on your Google-fu. Seriously, just Google `parse string java`. Founds LOTS of helpful results for your specific assignment including the duplicate I linked by just searching for `java split string by number of characters` – tnw Apr 14 '15 at 15:39
  • Not an answer but you can declare and initialize variables in one line, e.g. `String inputStr = keyboard.nextLine();` – Jyr Apr 14 '15 at 15:39

4 Answers4

0
int i;
for (i = 0; i < inputStr.length(); i += inputInt) {
    System.out.println(inputStr.substring(i, i + inputInt);
    // store the substring if you want to do something with them later
    // I am just printing them
}

if (i - inputInt < inputStr.length()) {
    // Print remainder, if there is any.
    System.out.println(inputStr.substring(i)); 
}
CletusW
  • 3,890
  • 1
  • 27
  • 42
Code Whisperer
  • 1,041
  • 8
  • 16
0

String.substring is definitely the way to go here. You'll want a loop, something like this:

for (int i = 0; i < s.length()-inputInt+1, i = i + inputInt) {
    String sub = s.substring(i, i+inputInt);
    System.out.println(sub);
}
Dave
  • 363
  • 2
  • 9
0

You can change the code Like This:

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 =0;i<inputStr.length();i+=inputInt){     
            if(i+inputInt < inputStr.length())
                System.out.println(inputStr.substring(i,i+inputInt));
            else
                System.out.println(inputStr.substring(i,inputStr.length()));
        }
    } 

for(int i =0;i<inputStr.length();i+=inputInt) Increments the loop the number entered by the user times. inputInt

The print the string from that i position to the i+inputInt to break the string.

The If and else loop used to avoid the StringIndexOutOfBoundsException beacuse consider if a user want break 5 letter word in 3 letters. The final string will have 2 letters and will throw StringIndexOutOfBoundsException Exception.

-----------------------------------EDIT --------------------------

To match the Example 2:

inputInt=keyboard.nextInt();
        for(int i =0;i<inputStr.length();i+=inputInt){     
            if(i+inputInt < inputStr.length())
                System.out.println(inputStr.substring(i,i+inputInt));

        }
Anjula Ranasinghe
  • 584
  • 1
  • 9
  • 24
0

Loop index need to incremented with inputInt. Try

    int i;
    for(i =0;i< inputStr.length()- inputInt;i+= inputInt){
        System.out.println(inputStr.substring(i, i + inputInt));
    }
    // For remaining substring
    if(i < inputStr.length()){
        System.out.println(inputStr.substring(i));
    }
Masudul
  • 21,823
  • 5
  • 43
  • 58