How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please.
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++;
}
}
}
}
Code:
First, save the below strings in a text file.
Input.txt
56
Rick
Daryl
Abhi
81
Jeet
Alex
Max
Carol
Vikas
Boss
Nitesh
62
Java Code:
StringSorting.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class StringSorting
{
public static void main(String[] args)
{
BufferedReader reader = null;
BufferedWriter writer = null;
//Create an ArrayList object to hold the lines of an input
file
ArrayList<String> lines = new
ArrayList<String>();
try
{
//Creating BufferedReader object to read the input file
reader = new BufferedReader(new FileReader("input.txt"));
//Reading all the lines of input file one by one and adding them
into ArrayList
String currentLine = reader.readLine();
while (currentLine != null)
{
lines.add(currentLine);
currentLine = reader.readLine();
}
//Sorting the ArrayList
Collections.sort(lines);
//Creating BufferedWriter object to write into the output
file
writer = new BufferedWriter(new FileWriter("output.txt"));
//Writing sorted lines into output file
for (String line : lines)
{
writer.write(line);
writer.newLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
//Closing the resources
try
{
if (reader != null)
{
reader.close();
}
if(writer != null)
{
writer.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Output Text File will be generated like below sorted strings.
Output.txt:
56
62
81
Abhi
Alex
Bhavani
Carol
Daryl
Jeet
Max
Nalini
Rick
Vikas
Screenshots:



Input Text file ScreenShot:

Output Text file Screenshot:

Please give a ThumbsUp.......
How can I make this program sort strings that are in a text file and not...
Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++)...
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
Need to code the HeapSort Algorithm in java. Most of the code is already done just need to code the "heapify" part of the heapsort algorithm. My heapify code is in bold below called "sink", but when I run it it gives me 2 errors with the string compare part saying: WordCountHeap.java:61: error: cannot find symbol if(l < k && String.Compare(pq[l], pq[n]) < 0) ^ symbol: method Compare(String,String) location: class String Please fix the code and the "heapify"...
Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...
Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...
Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...
Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter { public static void sort(int[] a) { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) { first[i] = a[i]; } for (int i = 0; i < second.length; i++) { second[i] =...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...
use the same code. but the code needs some modifications. so
use this same code and modify it and provide a output
Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...