Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use Scanner.nextInt() and Scanner.nextLine() together [duplicate]

I have to get a string input and an integer input, but there order of input should be that integer comes first then user should be asked for string input

Scanner in = new Scanner(System.in);


    input = in.nextLine();
    k = in.nextInt();

    in.close();

The above code works fine but if I take an integer input first like in the following code

Scanner in = new Scanner(System.in);

    k = in.nextInt();
    input = in.nextLine();


    in.close();

then it throws the java.lang.ArrayIndexOutOfBoundsException.

Here's the complete code of my source file:

import java.util.Scanner;

public class StringSwap {

public static void main(String args[]) {
    String input;
    int k;

    Scanner in = new Scanner(System.in);

    k = in.nextInt();
    input = in.nextLine();


    in.close();

    int noOfCh = noOfSwapCharacters(input);
    originalString(input, noOfCh, k);

}

public static int noOfSwapCharacters(String s) {

    char cS[] = s.toCharArray();
    int i = 0, postCounter = 0;
    while (cS[i] != '\0') {
        if (cS[i] != '\0' && cS[i + 1] != '\0') {

            cS[cS.length - 1 - postCounter] = '\0';

            postCounter++;

        }
        i++;
    }

    return postCounter;

}

public static void originalString(String s, int noOfCh, int k) {    
    int counter = 1, chCounter = 0;
    char cArray[] = s.toCharArray();
    String post = "";
    String pre = "";
    String finalString = "";
    char temp;

    for (int i = 1; i <= k; i++) {
        chCounter = 0;
        counter = 1;
        post = "";
        pre = "";

        for (int j = 0; j < cArray.length; j++) {

            if (counter % 2 == 0 && chCounter <= noOfCh) {
                temp = cArray[j];
                post = temp + post;
                cArray[j] = '\0';
                chCounter++;

            }
            counter++;

        }
        for (int h = 0; h < cArray.length; h++) {

            if (cArray[h] != '\0')
                pre = pre + cArray[h];

        }

        finalString = pre + post;
        for (int l = 0; l < finalString.length(); l++) {
            cArray[l] = finalString.charAt(l);

        }

    }

    System.out.println(finalString);
}

}

Kindly point out what I am doing wrong here.

like image 776
Umer Hassan Avatar asked Apr 12 '14 21:04

Umer Hassan


People also ask

Why can't nextLine be used after nextInt?

That's because the Scanner. nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner. nextLine returns after reading that newline.

Does Scanner nextInt go to next line?

The nextLine() method of java. util. Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.

Is Scanner nextLine blocking?

However, if you construct a Scanner using an InputStream that may require waiting for data to arrive (e.g., reading text from a remote client, waiting for a page to load so you can read its source, etc.) then Scanner#nextLine() will block because it needs to wait for enough information to build the next line to arrive.

What is the difference between next () and nextLine () methods of Scanner class?

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. next() : Finds and returns the next complete token from this scanner. nextLine() : Advances this scanner past the current line and returns the input that was skipped.

Why does scanner#nextint return a string instead of a newline?

Thats because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine Or, it would be even better, if you read the input through Scanner#nextLine and convert your input to integer using Integer#parseInt (String) method.

Why is scanner skipping nextline () after use of other next functions?

Why is Scanner skipping nextLine () after use of other next functions? The nextLine () method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.

What is nextline () in scanner?

Now Scanner#nextLine()simply collects and returns all characters until it finds line separators(or end of stream).

Why does the cursor remain just after nextint () method in scanner?

This issue occurs because, when nextInt () method of Scanner class is used to read the age of the person, it returns the value 1 to the variable age, as expected. But the cursor, after reading 1, remains just after it.


1 Answers

The problem is the '\n' character that follows your integer. When you call nextInt, the scanner reads the int, but it does not consume the '\n' character after it; nextLine does that. That is why you get an empty line instead of the string that you were expecting to get.

Let's say your input has the following data:

12345
hello

Here is how the input buffer looks initially (^ represents the position at which the Scanner reads the next piece of data):

1  2  3  4  5 \n h  e  l  l  o \n
^

After nextInt, the buffer looks like this:

1  2  3  4  5 \n h  e  l  l  o \n
              ^

The first nextLine consumes the \n, leaving your buffer like this:

1  2  3  4  5 \n h  e  l  l  o \n
                 ^

Now the nextLine call will produce the expected result. Therefore, to fix your program, all you need is to add another call to nextLine after nextInt, and discard its result:

k = in.nextInt();
in.nextLine(); // Discard '\n'
input = in.nextLine();
like image 191
Sergey Kalinichenko Avatar answered Oct 25 '22 02:10

Sergey Kalinichenko