Java case insensitive file

0 Vani Manikkam · January 28, 2016
how can i make a file case insensitive while opening it using java
for example this coding is sorting first uppercase and then lowercase
how do i change that:
mport java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MergeTest {
    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        String[] files = { "list.txt" };
        
 
       
        for (int x = 0; x < files.length; x++) {
            String[] testCase = readFile(files[x]);

            System.out.println("File: " + files[x]);
            System.out.println("Total words: " + testCase.length);
       
            MergeTest merge = new MergeTest();
            String[] sortTest = merge.mergeSort(testCase);
            
           
  for(int i=0;i<testCase.length;i++){

  System.out.println(sortTest);
   }
           
            System.out.println("Sorted completed");
            System.out.println("--------------------------\n\n");
        }
        
        System.exit(0);
    }
    
    public String[] mergeSort(String[] list) {
        String [] sorted = new String[list.length];
        if (list.length == 1) {
            sorted = list;
        } else {
            int mid = list.length/2;
            String[] left = null; 
            String[] right = null;
            if ((list.length % 2) == 0) {
                left = new String[list.length/2];
                right = new String[list.length/2];
            } else {  // 
                left = new String[list.length/2];
                right = new String[(list.length/2)+1];
            }
            int x=0;
            int y=0;
            for ( ; x < mid; x++) {
                left[x] = list[x];
            }
            for ( ; x < list.length; x++) {
                right[y++] = list[x];
            }
            left = mergeSort(left);
            right = mergeSort(right);
            sorted = merge(left,right);
            sorted = mergeArray(left,right);
        }
        
        return sorted;
    }
    
    private String[] merge(String[] left, String[] right) {
        List merged = new ArrayList();
        List lstLeft = new ArrayList(Arrays.asList(left));
        List lstRight = new ArrayList(Arrays.asList(right));
        int comp = 0;
        while (!lstLeft.isEmpty() || !lstRight.isEmpty()) {
            if (lstLeft.isEmpty()) {
                merged.add(lstRight.remove(0));
            } else if (lstRight.isEmpty()) {
                merged.add(lstLeft.remove(0));
            } else { // they both have elements, compare them
                comp = ((String) lstLeft.get(0)).compareTo((String) lstRight.get(0));
                if (comp > 0) {
                    merged.add(lstRight.remove(0));
                   
                } else if (comp < 0) {
                    merged.add(lstLeft.remove(0));
                } else { // they're equal -- just put one in
                    merged.add(lstLeft.remove(0));
                }
            }
            
        }
        return (String[]) merged.toArray(new String[merged.size()]);
    }
    
    private String[] mergeArray(String[] left, String[] right) {
        String[] merged = new String[left.length+right.length];
        int lIndex = 0;
        int rIndex = 0;
        int mIndex = 0;
        int comp = 0;
        while (lIndex < left.length || rIndex < right.length) {
            if (lIndex == left.length) {
                merged[mIndex++] = right[rIndex++];
            } else if (rIndex == right.length) {
                merged[mIndex++] = left[lIndex++];
            } else {  // they both have elements. Compare them
                comp = left[lIndex].compareTo(right[rIndex]);
                if (comp > 0) {
                    merged[mIndex++] = right[rIndex++];
                } else if (comp < 0) {
                    merged[mIndex++] = left[lIndex++];
                } else { // they're equal
                    merged[mIndex++] = left[lIndex++];
                }
            }   
        }
        return merged;
    }
    
    public static String[] readFile(String filename) throws IOException {
        BufferedReader inputStream = new BufferedReader(new FileReader(filename));
       
        List testList = new ArrayList();
        String[] sample = null;
        String l;
        while ((l = inputStream.readLine()) != null) {
            sample = l.split("\t");
            for (int x=0; x < sample.length; x++) {
                testList.add(sample[x]);
            }
        }
        inputStream.close();
        return (String[]) testList.toArray(new String[testList.size()]);
    }

}

sample view: 
Angiococcus disciformis
Azorhizobium
Azorhizobium caulinodans
Bacteria
Buchnera aphidicola
Cellulomonas gilvus
Cellvibrio
Corallococcus macrosporus
Cystobacteraceae
Dictyoglomus
Dictyoglomus thermophilum
Methylophilus
Methylophilus methylotrophus
Myxococcaceae
Myxococcales
Myxococcus
Myxococcus fulvus
Myxococcus xanthus
Pelobacter
Pelobacter carbinolicus
Phenylobacterium
Phenylobacterium immobile
Shewanella
Shewanella colwelliana
Shewanella hanedai
Shewanella putrefaciens
Stigmatella
Stigmatella aurantiaca
halophilic eubacterium
halophilic eubacterium NRCC 41227
Sorted completed
--------------------------


Post a Reply

Replies

Oldest  Newest  Rating
0 Nicholas Eason · January 28, 2016
String.toLowerCase() will return the whole String, but only lower-case characters. When you compare Strings, compare them like that, but don't delete the original one.
0 Vani Manikkam · January 29, 2016
i was suggested to used String.equals.ignoreCase  when compare
and it works :)
  • 1

Java / Android Development

107,070 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator