nj2012's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
13 (0.02 per day)
Joined:
23-April 12
Profile Views:
812
Last Active:
User is offline Jun 12 2012 07:29 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   nj2012 has not set their status

Posts I've Made

  1. In Topic: Problem with Inharatince

    Posted 25 May 2012

    Thank you very much... finally it is working :bananaman:
  2. In Topic: Problem with Inharatince

    Posted 24 May 2012

    I changed the body of check answer to the following code; but know for some reason it it always return true it does not check if (false) part, any help with this?

    {
    		boolean ok= false;
    		response = response.trim();
    		String correctAnswers [] = response.split(" ");
    		for(int i=0; i<correctAnswers.length;i++){
    		 answers.contains(correctAnswers[i]);
    		 if (true)
    		 {ok = true;}
                     if (false)
                     {ok = false;}
    }   
    return ok;
    		 }
    
    


    Thank you
  3. In Topic: Problem with odd Magic Square Program

    Posted 4 May 2012

    Thank you very much for your help :D
  4. In Topic: java.lang.ArrayIndexOutOfBoundsException - closest pair in array

    Posted 25 Apr 2012

    Thank you very much, I understood my mistake and the program is working fine.
  5. In Topic: BinarySearch in arry to find three elments that sums to zero

    Posted 24 Apr 2012

    Thank you very much for the help, I used the binary search with the range that you recommended, and checked the return index before I incremented my counter. Also, I noticed the problem with my sorting and modified it. The program is finally WORKING. Here is the modified code:


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Arrays;
    import java.util.Scanner;
    
    
    public class ThreeSum {
    	
    	public static void main(String args[]){
    		final int MAXNUMBERS = 1000;
    		int [] a = new int [MAXNUMBERS];
    		int valuesSize = 0;
    		int countTriples = 0;
    		String fname;
    		Scanner input = new Scanner(System.in);
    		
    		System.out.printf("Text File: ");
    		fname = input.next();
    		
    		// Check if file exist, if not throw an exception
    		Scanner infile = null;
    	    try {
    	    	infile = new Scanner(new File(fname));
    	    } 
    	    catch(FileNotFoundException e){
    	      System.out.printf("Unable to open input file %s\n", fname);
    	      System.exit(0);    
    	    }
    
    	    // Add elements to array and increment valuesSize
    	    while (infile.hasNextInt()){ 
    	    	a[valuesSize++] = infile.nextInt();	
    	    }
    	    
    
    	   Arrays.sort(a, 0 , valuesSize) ;
    
    	   
    	   
    	   for( int i=0; i<valuesSize; i++){
    		   for (int j =i+1; j<valuesSize; j++){	
    			   // Do a binary search to find the negative value of a[i]+a[j], which will result in a sum of 0
    			  if (j>i){  
    				 int k = Arrays.binarySearch(a, 0, valuesSize, -(a[i]+a[j]));	 
    				 if(k == -1){
    					 break;}
    				 if(k>j){
    			         if( a[k] == -(a[i] + a[j])){ 
    			        	 {
    								  countTriples++;}					  
    			   }
    			 }
    		  }
    		 }
    	   }
    	   
       System.out.println("Input file " +fname+ " has " +valuesSize+ " integers.");
       System.out.println("There were " +countTriples+ " triples with sum = 0." );
    	   
    	}
    }
    
    
    
    









    Thank you again :^:

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

nj2012 hasn't added any friends yet.

Comments

nj2012 has no profile comments yet. Why not say hello?