0

I have a situation where the program will take input for total no of string to be inputted. Once inputted it will print odd and even indexes of the string in one line separated by a space. For illustration this should be the output for the follwing input: 2 input ipt nu output otu upt

my logic seems fine but when I am trying to execute the program runs for only one time whatever be the input. Can anyone please let me know what am I missing here.

Code snippet

import java.util.Scanner;

public class javatest 
{
    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) 
    {
        String input_string;
        int inputs = scan.nextInt();//total inputs to be accepted
        int i=0;
        while(i<inputs) 
        {
            input_string = scan.nextLine();
            //for even places
            for (int j = 0; j < input_string.length(); j += 2) 
            {
                if (j % 2 == 0) 
                {

                        System.out.print(input_string.charAt(j));
                }
            }
            System.out.print(" ");
            //for odd places
            for (int k = 1; k < input_string.length(); k += 2) 
            {
                if (k % 2 == 1) 
                {
                    System.out.print(input_string.charAt(k));
                }
            }
            i++;
        }

    }
}

The above code is producing the output as
3
hello
hlo el
(execution ended)

avvinm
  • 21
  • 4
  • 1
    execution ended, or is it waiting for you to provide more input? – Stultuske Nov 06 '18 at 12:53
  • Your code looks OK, but as @Stultuske pointed out, you will need to provide a new string each time after the program prints out your modified input. – npinti Nov 06 '18 at 12:55
  • @Stultuske the issue is if I input 3 my code only accept one string and the execution ends abruptly whereas the desired behavior should be if 3 is the input it should accept 3 Strings as Input – avvinm Nov 06 '18 at 13:03
  • 2
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Progman Nov 06 '18 at 13:06
  • @avvinm I just copy pasted your code and ran it here. you are mistaken. it waits for more input (though only twice). you need to add a scan.next(); after your scan.nextLine(); (and some printouts "ENTER DATA") to make it more clear to you that the execution isn't terminated – Stultuske Nov 06 '18 at 13:12
  • 1
    You should **follow the Java Naming Conventions**: class names are always written in PascalCase (first letter is uppercase), variable and method names are always in camelCase (first letter lowercase). – MC Emperor Nov 06 '18 at 13:14
  • @MCEmperor though that would make the code more readable, it wouldn't really change the behaviour – Stultuske Nov 06 '18 at 13:17
  • @Stultuske It indeed does not. But it will save a lot of hurt in the future, so it's best to mention it as soon as possible. – MC Emperor Nov 06 '18 at 13:30

2 Answers2

0

The issue is that scan.nextInt() does not read the enter pressed while inputing the number. Due to this issue your program runs 1 iteration lesser than the input. I ran your exact code without any modification and it runs twice for input 3.

The first call to scan.nextLine() gives an empty string.

The alternative can be replacing

scan.nextInt() with Integer.valueOf(scan.nextLine());

which will read the enter/new line character also.

harsh
  • 1,119
  • 10
  • 11
0

By pressing enter upon entering the amount of inputs you are creating a newline \n character which is consumed by the input_string = scan.nextLine(); in your while loop. So directly after entering the amount of inputs the while loop will increment the value of i to 1 as it processes the \n character.

As a workaround you could fetch the amount of expected inputs as a String and parse it to an int like this:

int inputs = 0;
try {
    inputs = Integer.parseInt(scan.nextLine());
} catch (NumberFormatException e) {
    // handle exception gracefully, maybe by showing a message to the user.
}

Also to make it more clear for the user to understand that an input is expected you might want to add a message that makes it clear a new input is expected:

System.out.println("Please provide the next input: ");
input_string = scan.nextLine();
Bas de Groot
  • 628
  • 3
  • 16