0

So , I am trying to create a program which calculates the score of an exam . What we have to do is give a pattern as a string consisting X's and 0's i.e 000XXX000XXX, where the value of 'X' is 0 and the value of each zero is 1 and for every consecutive '0' the value increases by 1 . If suppose there are 2 or more consecutive 0's and then an 'X' the value of '0' is reset to 1.if the program seems common to you , then , yes this is a problem from an OJ and it was given to me by a senior from my university to solve.Now the thing is I have figured out how the code works and solved the problem.But there seems to be an issue in the code.

package javaapplication4;

import java.util.Scanner;

public class JavaApplication4 {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T, score = 0, f = 0, g = 0;
    String str;
    int len;
    T = sc.nextInt();

    for (int i = 1; i <= T; i++) {
        str = sc.nextLine();
        len = str.length();

        for (int j = 0; j < len; j++) {
            if (str.charAt(j) == '0') {
                f++;
                score = score + f;

            }
            else if(str.charAt(j) == 'X')
            {
                f = 0;
                score = score + g;
            }
        }

        System.out.println(score);

    }
}

}

As you can see from the code , I first give an Input for the number of test cases and as soon as I press enter , the code displays the value of score (which is 0) automatically without doing any think inside the for loop. I have rechecked all the curly braces, but I cannot find the bug in the code. I would be happy if I could get some help.

Output:
4
0
Zarar Mahmud
  • 113
  • 6
  • You miss `sc.nextLine();` after `T = sc.nextInt();` – Jens Mar 27 '17 at 14:25
  • At least for me, your explanations are confusing. Maybe you should instead provide an enhanced example. – GhostCat Mar 27 '17 at 14:25
  • BTW: Pay attention to the Java naming convention. variables should start with lower character – Jens Mar 27 '17 at 14:26
  • @Jens thanks I knew whatever the problem it was pretty minute , but I could not figure out what exactly to search so I had to come to her for the answer :) – Zarar Mahmud Mar 27 '17 at 14:30

1 Answers1

0

The sc.nextInt() causes the sc.nextLine() to be triggered so you get the output of a empty string that's why it's zero, by using sc.nextLine() for input of your test case number you can prevent this:

        int score = 0;
        System.out.println("Enter test case:");
        int testCase= Integer.parseInt(sc.nextLine());

        for (int i = 1; i <= testCase; ++i)
        {
            System.out.println("Enter pattern:");
            String str = sc.nextLine();
                for (int j = 0; j < str.length(); j++)
                {
                    if (str.charAt(j) == '0')
                    {
                        score += 1;

                    }
                    else if (str.charAt(j) == 'X')
                    {
                        score += 0;
                    }
                }

                System.out.println(score);

                score = 0; // reset score to zero for the next test case 
        }

See this link regarding the sc.nextInt() issue : Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
Chester Cobus
  • 549
  • 3
  • 11