0

Here is the problem that I am trying to solve:

Task Given a string, S, of length N that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as space-separated strings on a single line.

Input Format

The first line contains an integer, T (the number of test cases). Each line i of the T subsequent lines contain a String, S.

Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

My problem with the code is that the first line (Hce akr) is getting printed but the next one is not. I read some similar problems and put in a nextLine() before the start of the loop (inputting the Strings) but then only the second output is displayed (Rn ak). I don't understand where I am going wrong.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T=sc.nextInt();
    if((T>=1)&&(T<=10))
        {
        String s[]=new String[T];
        int i;
        for(i=0;i<T;i++)
          {
           s[i]=sc.nextLine();                
        }
        int flag=0;
        for(i=1;i<T;i++)
            {
            for(int j=0;j<s[i].length();j=j+2)
                {
                System.out.print(s[i].charAt(j));
                if((j+2)>(s[i].length()-1))
                {
                    System.out.print(" ");
                    j=-1;
                    flag++;
                }
                if(flag==2)
                    break;
            }
                System.out.println();

           }
    }



}
Nikhil_10
  • 103
  • 1
  • 8
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – OneCricketeer Dec 01 '16 at 16:10
  • 1
    to read integer try `T=Integer.parseIn(sc.nextLine())` instead of `T=sc.nextInt();` – nafas Dec 01 '16 at 16:12
  • @nafas if i do that and keep i=0 as Gatusko says below, I get this: Hce akr R (R on the next line, of course) and if i keep it =1 as in the above code i get the second line of output only – Nikhil_10 Dec 01 '16 at 16:18

2 Answers2

0

It's because of your for

for(i=1;i<T;i++)

Start with

for(i=0;i<T;i++)
Gatusko
  • 2,253
  • 1
  • 13
  • 23
0

because of your program logic it can only reads the number of line you specify as input. since you specify 2 as input in the beginning that's why it only ask you to enter one more line only. So it does not even taking Rank as input that's why you don't see it.if You change your first input to 3, you will be able to enter and see two more lines.

hhafeez
  • 63
  • 8
  • I didn't get it...The input 2 will tell the loop to input two lines. Will the nextLine() include the space(newline) on the same line after 2? Because I tried writing sc.newLine() first to get rid of the newline but it didn't work as I have mentioned in the question. – Nikhil_10 Dec 01 '16 at 16:52
  • when you entered 2 , the scanner was already on line 1 due to `sc.nextInt()` and when you execute 'sc.nextLine()' it returns you the rest of the current line (which is empty after 2). so it does not take your second "third" input line. Input 2 and Hacker are two lines basically. – hhafeez Dec 01 '16 at 17:16
  • I thought that was the problem too but then I added a line with just the command ' sc.nextLine(); ' before inputting the Strings only to get an output giving me not two empty lines but Rn ak – Nikhil_10 Dec 01 '16 at 17:21
  • @Tikli basically using `sc.nextLine()` after `sc.nextInt()` does not return nextLine since you have not finished reading the line you already on. It returns you the rest of the current line. – hhafeez Dec 01 '16 at 17:23
  • @Tikli to prove this point try not to take integer input from scanner and hard code as `int T= 2`; you will see you program works fine, and you will see both of your lines for Hacker and Rank – hhafeez Dec 01 '16 at 17:26
  • `Scanner sc = new Scanner(System.in); int T = sc.nextInt(); sc.nextLine(); if ((T >= 1) && (T <= 10))` – hhafeez Dec 01 '16 at 17:38