0

I'm scanning 2 strings during each iteration and storing it in s and t. Only during the first iteration, the first string that I scan is getting stored in t and not in s (I got to know this by debugging in eclipse). During successive iterations the piece of code works fine. I'm not able to understand what is going on during the first iteration. Please help me. Thanks.

import java.io.*;
import java.util.*;

public class ResidentInfo {

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner scan = new Scanner(System.in);
    int i,n;
    n = scan.nextInt();
    for(i=0 ; i<n ; i++)
    {
        int sl,tl,j,k;
        String s, t;
        boolean flag = false;

        s = scan.nextLine();
        t = scan.nextLine();
        sl = s.length();
        tl = t.length();
        char[] sa = new char[sl];
        char[] ta = new char[tl];
        sa = s.toCharArray();
        ta = t.toCharArray();
        for(j=0 ; j<sl ; j++)
        {
            for(k=0 ; k<tl ; k++)
            {
                if(sa[j]==ta[k])
                {
                    flag = true;
                    break;
                }                    
            }
            if(flag)
            {
                break;
            }
        }
        if(flag)
        {
            System.out.println("YES");               
        }
        else
        {
            System.out.println("NO");
        }
    }
}
}
Anand Kumar
  • 169
  • 1
  • 10
  • in first iteration, what is stored in `s`. Also to debug the issue, you can add several print statements all over your code (remove them after you find the issue).. – Jos Sep 22 '16 at 15:21
  • @redflar3 I'm getting string input from the user in keyboard and storing it in s. But only during first iteration the code inside the for loop is not working properly. Nothing is stored in s in first iteration. – Anand Kumar Sep 22 '16 at 15:23
  • 1
    What does your program do? What input are you typing? What are the expected results, and what are the actual results? – John Kugelman Sep 22 '16 at 15:24
  • @JohnKugelman Actually, s and t are two strings. if there is a common substring, the output is YES or else NO. for loop is used for repeating this process for n number of pairs. From second iteration onwards first string of the pair is stores in s and second one is stored in t correctly. But only in the first iteration, only NULL is stored in s and the string that I type is stored in t. This is wrong. – Anand Kumar Sep 22 '16 at 15:28
  • You are asking for more input inside the loop. You should always inform the user what type of input the program is asking for. So before All of your scan.next* statements you should say something like System.out.print("please enter a int"); You problem is that you are asking for more input inside the loop. – Sedrick Sep 22 '16 at 15:28
  • @SedrickJefferson I tried what you said now. Still same problem exists. – Anand Kumar Sep 22 '16 at 15:31
  • Where you have nextLine use next. I don't know why this is a problem. – Sedrick Sep 22 '16 at 15:39
  • @SedrickJefferson It works if I use next instead of nextLine. Thanks man. Just for some knowledge, may I know what exactly is the difference between next() and nextLine() – Anand Kumar Sep 22 '16 at 15:43
  • Why are you using a nested for loop? It looks like you are trying to write a program to compare two string. I am a fairly new programmer, but you could have taken a better approach if that is what you are trying to do. – Sedrick Sep 22 '16 at 15:45
  • Next line grabs everything up to \n or newline character. Next grabs up to the first white space. – Sedrick Sep 22 '16 at 15:46
  • @SedrickJefferson I'm very new to programming. I would be thankful to you if you can tell me what that better approach is. Thanks in advance. – Anand Kumar Sep 22 '16 at 15:48
  • I edited my answer. You should first check to see if the strings lengths are the same. If they are not the same, then the strings are not equal. If they are the same, you should check each character in one string against the character in the second string at the exact same location. If all characters are equal then the strings are equal. – Sedrick Sep 22 '16 at 15:59
  • If my solutions is correct you should mark it as correct. – Sedrick Sep 22 '16 at 16:03

1 Answers1

0

The first thing this code does is determine if the two strings' lengths are equal. If the strings' lengths are not equal, the code prints no. If the lengths are equal, the code then checks each character in the exact same index in the strings, to see if they are equal. If the characters at a specific index is not equal, the code breaks the loop and prints no. If all of the characters at each index are equal the code prints yes.

You asked a question about next() and nextLine(). Try: Question asked already.

Scanner scan = new Scanner(System.in);
System.out.print("enter a number: ");
int n = scan.nextInt();


for(int i=0; i < n; i++)
{
    boolean flag = true;

    System.out.print("enter something for s: ");
    String s = scan.next();

    System.out.print("enter something for t: ");
    String t = scan.next();

    if(s.length() == t.length())
    {
        for(int j = 0; j < s.length(); j++)
        {
            if(s.charAt(j) != t.charAt(j))
            {
                flag = false;
                break;
            }
        }
        if(flag)
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("NO");
        }
    }
    else
    {
        System.out.println("NO");
    }
Community
  • 1
  • 1
Sedrick
  • 10,209
  • 3
  • 35
  • 49