-2

In my java code the line String st = sc.nextLine(); is not taking input when the code is running and when instead I use sc.next(); the code works properly can you please tell me why sc.nextLine(); is not working

 import java.lang.*;
 import java.util.*;

/**?Chef wrote some text on a piece of paper and now he wants to know how 
many holes are in the text. What is a hole? If you think of the paper as 
the plane and a letter as a curve on the plane, then each letter divides
the plane into regions.For example letters "A", "D", "O", "P","R" divide
the plane into two regions so we say these letters each have one hole. 
Similarly, letter "B" has 2 holes and letters such as "C", "E", "F","K" 
have no holes. We say that the number of holes in the text is equal to 
the total number of holes in the letters of the text. 
Help Chef to determine how many holes are in the text.

Input

First line contains a single integer T <= 40, the number of test cases. 
T test cases follow. The only line of each test case contains a non-empty
text composed only of uppercase letters of English alphabet. The length 
of the text is less then 100. There are no any spaces in the input.

 Output

For each test case,output a single line containing number of holes in the corresponding txt
Example

Input:
2
CODECHEF
DRINKEATCODE

Output:
2
5
*/

class Holes {
    public static void main(String[] args) {

        int i, Testcase;
        int holes, space = 0, j;

        Scanner sc = new Scanner(System.in);
        Testcase = sc.nextInt();
        for (i = 0; i < Testcase; i++) {
            holes = 0;
            space = 0;

            String st = sc.nextLine();

            if (st.length() < 100) {
                char[] letter = st.toCharArray();
                for (j = 0; j < st.length(); j++) {
                    if (letter[j] == ' ') {
                        space++;
                    }
                }

                for (j = 0; j < st.length(); j++) {
                    if (space == 0) {
                        if (letter[j] == 'A' || letter[j] == 'D'
                                || letter[j] == 'O' || letter[j] == 'P'
                                || letter[j] == 'R') {
                            holes = holes + 1;

                        }

                        if (letter[j] == 'B') {
                            holes = holes + 2;
                        }
                    }
                }

            }
            if (st == st.toUpperCase() && space == 0) {
                System.out.println(holes);
            }
        }
    }
}
Hitham S. AlQadheeb
  • 13,613
  • 3
  • 47
  • 74
Jai
  • 2,701
  • 2
  • 14
  • 23
  • st==st.toUpperCase() - use the equals method to compare the value of objects – Stultuske Dec 25 '15 at 09:30
  • 3
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Stultuske Dec 25 '15 at 09:31
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Hitham S. AlQadheeb Dec 25 '15 at 12:57

2 Answers2

1

That's 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

Workaround:

  • Either fire a blank Scanner#nextLine call after Scanner#nextInt to consume rest of that line including newline

Check this out.

Community
  • 1
  • 1
Nighthacks
  • 390
  • 3
  • 13
1

As Nighthacks mentioned, Scanner.nextInt will not advance to the next input (Read https://stackoverflow.com/a/13102066/643500)

Try it this way:

public class Holes {

    public static void main(String[] args) {

        int holes;

        Scanner sc = new Scanner(System.in);

        int numberOfCases = sc.nextInt(); // Get input as int - not going to
                                            // advance

        String[] testCases = new String[numberOfCases];

        String line = sc.nextLine(); // Move to next

        for (int i = 0; i < numberOfCases; i++) {
            line = sc.nextLine();// Read input as line
            testCases[i] = line;
        }

        sc.close();

        for (String aCase : testCases) {

            holes = 0;

            if (aCase.length() < 100 && !aCase.contains(" ")) {

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

                    char letter = aCase.charAt(j);

                    if (letter == 'A' || letter == 'D' || letter == 'O'
                            || letter == 'P' || letter == 'R') {
                        holes++;
                    }

                    if (letter == 'B') {
                        holes = holes + 2;
                    }
                }

                System.out.println(holes);
            }
        }
    }
}
Community
  • 1
  • 1
Hitham S. AlQadheeb
  • 13,613
  • 3
  • 47
  • 74