0

I need to read in a text file and then sort each strings alphabetically into array (don't need to worry about case sensitivity).

I need to use the arrays to to do binary search later.

Here is the code:

public class project2 {
    public static void main( String[] args ) throws IOException {
        String[] list = { "" }; 
        list = load_list( "wordlist.txt" );
        ArrayList<String> words =new ArrayList<String>(Arrays.asList(list));  

        File fileReader = new File( "wordlist.txt" );
        Scanner inputFile = new Scanner( fileReader );

        Collections.sort(words,String.CASE_INSENSITIVE_ORDER);

    }
    public static String[] load_list( String wordlist ) throws IOException {
        File fileReader = new File( "wordlist.txt" );
        Scanner inputFile = new Scanner( fileReader );
        List<String> L = new ArrayList<String>();
        while ( inputFile.hasNextLine() ) {
            L.add(inputFile.nextLine());
        }
        return L.toArray(new String[L.size()]);
    }
}

The first block of code is my attempt to read and sort them

the 2nd block is to read each line of strings

Im ahead of myself but can someone give me hint on using the result of these codes to do sequential/binary search?

4
  • What does "doesn't work" mean? We are not mind readers. Please provide SPECIFIC error messages, etc. Commented Dec 8, 2013 at 3:54
  • You've String[] return type at method signature, but didn't return anything. Commented Dec 8, 2013 at 3:55
  • Well, your first problem is that the method, load_list is declared to return an array of String, but you return nothing. That should not even compile. Commented Dec 8, 2013 at 3:55
  • 1
    The type returned by Arrays.asList cannot be cast to java.util.ArrayList. You need to use List<String> instead. Commented Dec 8, 2013 at 4:01

1 Answer 1

1

include following imports.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

and

change

ArrayList<String> words = Arrays.asList(list);  

to

ArrayList<String> words =new ArrayList<String>(Arrays.asList(list));  

if you want to ignore case use like below.

  Collections.sort(words,String.CASE_INSENSITIVE_ORDER); 

update

change your load_list() method like this.

  public static String[] load_list( String filename ) throws IOException {
       File fileReader = new File( filename );
       Scanner inputFile = new Scanner( fileReader );
       List<String> L = new ArrayList<String>();
       while ( inputFile.hasNextLine() ) {
           L.add(inputFile.nextLine());
       }
       return L.toArray(new String[L.size()]);
}

change your main() method like this.

public static void main(String[] args) throws IOException {
      String[] list = { "" }; 
      list = load_list( "wordlist.txt" );
      ArrayList<String> words = new ArrayList<String>(Arrays.asList(list)); 
      Collections.sort(words,String.CASE_INSENSITIVE_ORDER); 
}

and include following imports

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

to print the ArrayList use like below

for(String s: list){
     System.out.println(s);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much for your input. I have 1 more question. Can you give me some hints on how to use what I have so far to do a sequential/binary search? I need read in strings from file2 and check if they exist in the array above. I also need to keep track of peeks. I dont want straight answer but I want to get a head start and work on the code myself. thanks again!
@JimmyBob try this one Collections.binarySearch(list, key) else you implement ur own

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.