CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Java Programming > Java Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Java Programming Ask your Java programming question and help out others with theirs.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 7th, 2009, 01:13 AM
    Welshh Welshh is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 1
    Welshh is an unknown quantity at this point (<10)
    Array Binary Seach Issue

    Hey,

    Hey, so the little chunk of code im trying to write reads in a text file and stores the 2 columns in an array. The data in the file is:
    Code:
    A	.-
    B	-...
    C	-.-.
    D	-..
    E	.
    F	..-.
    G	--.
    H	....
    I	..
    J	.---
    K	-.-
    L	.-..
    M	--
    N	-.
    O	---
    P	.--.
    Q	--.-
    R	.-.
    S	...
    T	-
    U	..-
    V	...-
    W	.--
    X	-..-
    Y	-.--
    Z	--..
    1	.---
    2	..---
    3	...--
    4	....-
    5	.....
    6	-.....
    7	--...
    8	---..
    9	----.
    0	-----
    and here is the code
    Code:
    import java.io.*;
    import java.util.*;
    
    public class test {
    
        public static void main(String[] args) throws IOException{
    
        	int 	index;
        	
        	File morseData = new File("Morse.dat");
        	
        	Scanner reader = new Scanner(morseData);
        	
        	String[] morseCode = new String[36];
        	String[] morseChar = new String[36];
        	
        	for(index = 0; index < 36; index++)
        	{
        		morseChar[index] = reader.next();
        		morseCode[index] = reader.next();
        	}
        	
        	reader.close();
        	
        	index = Arrays.binarySearch(morseCode, "-----");
        	
        	System.out.print(index);
    
        }
        
    }
    Now unless im missing something, it should return that "-----" is on the last row so index 35 in the array. However it just returns -1. Ive tested by printing out both arrays out to make sure they properly populated and they did, I'm not sure if im just missing something, any help would be great.
    Reply With Quote
      #2    
    Old November 7th, 2009, 04:29 AM
    dlorde dlorde is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 1999
    Location: UK
    Posts: 9,159
    dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)
    Re: Array Binary Seach Issue

    A binary search needs a sorted array - as the Javadocs for the method clearly say. For your requirement, a Map (HashMap?) would be a better solution than two arrays.

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anon.
    __________________
    Please use [CODE]...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Java Programming > Java Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 07:16 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009