Question

How can I make this program sort strings that are in a text file and not...

How can I make this program sort strings that are in a text file and not have the user type them in? import java.util.*;
public class MergeDemo
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.print("How many lines to be sorted:");
int size=input.nextInt();
String[] lines=new String[size];
lines[0]=input.nextLine();
System.out.println("please enter lines...");
for(int i=0;i {
lines[i]=input.nextLine();
}
System.out.println();
System.out.println("Lines Before Sorting:");
System.out.println(Arrays.toString(lines));
mergeSort(lines);
System.out.println();
System.out.println("Lines after Sorting:");
System.out.println(Arrays.toString(lines));
}

public static void mergeSort(String[] s)
{
if(s.length>1)
{
String[] left=Arrays.copyOfRange(s,0,s.length/2);
String[] right=Arrays.copyOfRange(s,s.length/2,s.length);
mergeSort(left);
mergeSort(right);
merge(s,left,right);
}
}
public static void merge(String[] result, String[] left, String[] right)
{
int i1 = 0;
int i2 = 0;
for (int i = 0; i < result.length; i++)
   {
   if (i2 >= right.length || (i1 < left.length &&left[i1].compareToIgnoreCase(right[i2])<0))
   {
result[i] = left[i1];
i1++;
}
   else
   {
result[i] = right[i2];
i2++;
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class MergeDemo {
   public static void main(String args[]) throws Exception {
       Scanner input = new Scanner(new File("lines.txt"));
       ArrayList<String> s = new ArrayList<String>();
       while (input.hasNextLine()) {
           s.add(input.nextLine());
       }
       String lines[] = (String[]) s.toArray();
       System.out.println();
       System.out.println("Lines Before Sorting:");
       System.out.println(Arrays.toString(lines));
       mergeSort(lines);
       System.out.println();
       System.out.println("Lines after Sorting:");
       System.out.println(Arrays.toString(lines));
   }

   public static void mergeSort(String[] s) {
       if (s.length > 1) {
           String[] left = Arrays.copyOfRange(s, 0, s.length / 2);
           String[] right = Arrays.copyOfRange(s, s.length / 2, s.length);
           mergeSort(left);
           mergeSort(right);
           merge(s, left, right);
       }
   }

   public static void merge(String[] result, String[] left, String[] right) {
       int i1 = 0;
       int i2 = 0;
       for (int i = 0; i < result.length; i++) {
           if (i2 >= right.length || (i1 < left.length && left[i1].compareToIgnoreCase(right[i2]) < 0)) {
               result[i] = left[i1];
               i1++;
           } else {
               result[i] = right[i2];
               i2++;
           }
       }
   }
}

//Instructions: write the lines into the linex.txt and put the file at same location where your java file is located

Note : If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
How can I make this program sort strings that are in a text file and not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT