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

class Telefon {
      
      public static void main(String[] args) throws Exception {
	    
	    /* IO */
	    
	    BufferedReader sisend = new BufferedReader(new FileReader("telefon.sis"));  // IN
	    PrintWriter valjund = new PrintWriter(new BufferedWriter(new FileWriter("telefon.out")));  // Out
	    
	    int n = Integer.parseInt(sisend.readLine()); // Reading first integer
	    
	    String abi;
	    String abi2;
	    
	    String[] numbrid = new String[n]; // Numbers for array
	    
	    for ( int l = 0; l < n; l++ ) {
		  
		  numbrid[l] = sisend.readLine();
		  
		  for ( int j = 1; j < numbrid.length - 1; j++ ) {
			
			if ( numbrid[l].equals( numbrid[j] ) && l != j ) {
			      
			      valjund.println("JAH"); // Yes
			      abi = numbrid[l];
			      abi2 = numbrid[j]; 
			      valjund.println(abi);
			      valjund.println(abi2);   
			}
			
			else {
			      valjund.println("EI"); // NO
			      valjund.close();
			}
		  } 
	    } 
	    sisend.close();  // Input close
	    valjund.close(); // Output close
      }   
}

Hey, here is my problem:

Input:
3
12345
123456
123

It is necessary to check number eguals next line numbers.
Example 12345 equals 123456.

4
123
1223
146777
1467778

Example 146777 equals 1467778.

And output

YES
146777
1467778

Sorry my bad .........

Recommended Answers

All 7 Replies

Why don't you use: startsWith, instead of equals. Of course that means that for these the methods will return true:

1234
12345
"12345".startsWith("1234") = TRUE

1
123456
"123456".startsWith("1") = TRUE

I took a closer look to your code and here is what I suggest.

First you need to read all the values of the array and then check for equality. You do this:

for ( int l = 0; l < n; l++ ) {
        numbrid[l] = sisend.readLine();
	for ( int j = 1; j < numbrid.length - 1; j++ ) {

You read the 1st number but then you loop the entire array. But the rest of the elements of the array are null.

I suggest to do something like this:

for ( int l = 0; l < n; l++ ) {
        numbrid[l] = sisend.readLine();
}

for ( int i = 0; i < n; i++ ) {
   for ( int j = [B](i+1)[/B]; j < n; j++ ) {
       // compare the values
}
}

Also check the index of the second (inner loop) I think that it is more appropriate. Remember that with each loop the first index increases by one. So if you want the second loop to look after that index don't set it to 1 but to i+1.

Thanks javaAddict, problem is solved

This code works.

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

class Telefon {
      
      public static void main(String[] args) throws Exception {
	    
	    /* IO */
	    
	    BufferedReader input = new BufferedReader(new FileReader("telefon.sis"));  
	    PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("telefon.out")));  
	    
	    int n = Integer.parseInt(input.readLine());	
	    
	    String[] numbers = new String[n];	
	    
	    for ( int j = 0; j < n; j++ ) {	
		  
		  numbers[j] = input.readLine();	 
	    }
	    
	    for ( int i = 0; i < n; i++ ) {
		  
		  for ( int j = i + 1; j < n; j++ ) {
			
			if ( numbers[j].startsWith( numbers[i] ) && i != j ) {
			      
			      output.println("JAH");	
			      output.println( numbers[i] );
			      output.println( numbers[j] );
			      output.close();
			}
		  } 
	    }
	        
	    output.println("EI");	 
	    input.close();
	    output.close();	
	    
      }  
}

A few corrections.
Since you do this at the loop: (j=i+1)

for ( int i = 0; i < n; i++ ) {
		  for ( int [B]j = i + 1[/B]; j < n; j++ ) {
			if ( numbers[j].startsWith( numbers[i] ) && [B]i != j[/B] ) {

You don't need to check if the i!=j. It will never happen. You make sure of that by starting j to be "i+1".
Another reason you initialize the j that way is so you will not compare numbers that have already been compared.
Imagine i=1,j=2 and comparing number[1],number[2] and then at the next loop i=2,j=1 and comparing number[2],number[1]
You avoid that

Also don't close the output in the if. Close them at the end of your program.
Also if the "YES" is printed, have a new variable take value true.
With your code, you will print "YES" in the if, but when the loops finish you will also print "NO". Have that variable take value "true" inside the if, and outside the "for-s" print "No" only if the variable is false.

And lastly don't use throws at the main method.:

// declared outside
  BufferedReader input = null;  
  PrintWriter output = null;
try {
   // initialized inside the try
   input = new BufferedReader(new FileReader("telefon.sis"));  
   output = new PrintWriter(new BufferedWriter(new FileWriter("telefon.out")));
  // your code here

  // close them last
  input.close();
  output.close();
} catch (Exception e) {
   // for debuging:
   // e.printStackTrace();

   System.out.println(e.getMessage());
}

Closing input because I need only one pair. For example:

3
123
1234
12345

Output

YES
123
1234

or

1234
12345

or

123
12345

One pair, all couples do not need to be issued.

Closing input because I need only one pair. For example:

3
123
1234
12345

Output

YES
123
1234

or

1234
12345

or

123
12345

One pair, all couples do not need to be issued.

In that case break from the loops or exit the applicaton.

What happens when you call output.close() and then output.println("") afterwards?
Because that is what you do when you finish with the loops.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.