Question

In: Computer Science

The program should be able to do the following: In Java accepts one command line parameter....

The program should be able to do the following: In Java

  1. accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows:

    1. There will be multiple lines in the file (number of lines unknown).

    2. Each line will contain multiple integers, separated by a single whitespace.

  2. reads the integers from the text file in part a into an array of integers.

  3. sort the integers in ascending order, and then prints out a sorted version of these integers, one per line

  4. Implements the selection sort algorithm.

Solutions

Expert Solution

Code


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class Main {
public static void main(String[] args)
{
int []arr=new int[1000];
int count=0;
String line;
BufferedReader reader;
try
{
reader = new BufferedReader(new FileReader(args[0]));
line = reader.readLine();
while (line != null)
{   
// read next line
String []value=line.split(" ");
for(int i=0;i<value.length;i++)
{
arr[count]=Integer.parseInt(value[i]);
count++;
}
       line = reader.readLine();
}
reader.close();
   }
catch (IOException e)
{
System.out.println(args[1]+" file not found.");
   }
System.out.println("Original Array is: ");
printArray(arr,count);
selectionSort(arr, count);
System.out.println("\n\nAfter sorting array is: ");
printArray(arr,count);
}
public static void selectionSort(int[] arr,int size)
{
for (int i = 0; i < size - 1; i++)
{
int index = i;
for (int j = i + 1; j < size; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];   
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}

private static void printArray(int[] arr, int count)
{
for(int i=0;i<count;i++)
System.out.println(arr[i]);
}
}
Numbers.txt file

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts one integer parameter and returns that value raised to the third power. Write a method called randomNumber that returns a random floating-point number in the range of [-20.0, 50.0). (hints: use Random class in the method) Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method) Overload findMax that accepts one...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT