Question

I need to write a program in java that reads a text file with a list...

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);

}

// LOAD FILE INTO ARR USING INSERINORDER

Scanner infile = new Scanner( new File( args[0] ) );

int[] arr = new int[INITIAL_CAPACITY];

int count= 0;

while (infile.hasNextInt())

{

if ( count == arr.length ) arr = upSize( arr );

insertInOrder( arr, count, infile.nextInt() );

++count; // WE JUST ADDED A VALUE - UP THE COUNT

}

infile.close();

printArray( "SORTED ARRAY: ", arr, count );

} // END MAIN

// ################################################################

// USE AS IS - DO NOT MODIFY

static void printArray( String caption, int[] arr, int count )

{

System.out.print( caption );

for( int i=0 ; i<count ;++i )

System.out.print(arr[i] + " " );

System.out.println();

}

// YOU WRITE THIS METHOD - DO NOT MODIFY THIS FILE ANYWHERE ELSE

// ################################################################

static void insertInOrder( int[] arr, int count, int key )

{

}

static int[] upSize( int[] fullArr )

{

int l = fullArr.length * 2;

// CHANGE TO YOUR RETURN STATEMENT

}

} // END LAB3

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

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);
}
// LOAD FILE INTO ARR USING INSERINORDER

Scanner infile = new Scanner( new File( args[0] ) );
int[] arr = new int[INITIAL_CAPACITY];
int count= 0;
while (infile.hasNextInt())
{
if ( count == arr.length ) arr = upSize( arr );
insertInOrder( arr, count, infile.nextInt() );
++count; // WE JUST ADDED A VALUE - UP THE COUNT
}
infile.close();
printArray( "SORTED ARRAY: ", arr, count++ );

} // END MAIN
// ################################################################

// USE AS IS - DO NOT MODIFY
static void printArray( String caption, int[] arr, int count )
{
System.out.print( caption );
for( int i=0 ; i<count ;++i )
System.out.print(arr[i] + " " );
System.out.println();
}

// YOU WRITE THIS METHOD - DO NOT MODIFY THIS FILE ANYWHERE ELSE
// ################################################################
static void insertInOrder( int[] arr, int count, int key )
{

int flag=0;
for(int i=0;i<count;i++){
if(arr[i]>key){
int temp1=arr[i];
int temp2;
for(int j=i;j<count;j++){
temp2=arr[j+1];
arr[j+1]=temp1;
temp1=temp2;
  
}
arr[i]=key;
flag=1;
break;
}
}
if(flag == 0){
arr[count] = key;
}
}
static int[] upSize( int[] fullArr )
{
int l = fullArr.length * 2;
int[] modArr =new int[l];
for(int i=0;i<fullArr.length;i++){
modArr[i]=fullArr[i];
}
// CHANGE TO YOUR RETURN STATEMENT
return modArr;
}
} // END LAB3

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
I need to write a program in java that reads a text file with a list...
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
  • JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....

    JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: ';' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: <identifier> expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:94: error: illegal start of type return true; ^ Project4.java:98: error: class, interface, or enum expected static int bSearch(int[] a, int count, int key) ^ Project4.java:101: error: class, interface, or...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Hi I need some help on this lab. The world depends on its successfull compilation. /*...

    Hi I need some help on this lab. The world depends on its successfull compilation. /* Lab5.java Arrays, File input and methods Read the comments and insert your code where indicated. Do not add/modify any output statements */ import java.io.*; import java.util.*; public class Lab5 { public static void main (String[] args) throws Exception { final int ARRAY_MAX = 30; // "args" is the list of tokens you put after "java Project3" on command line if (args.length == 0 )...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

    Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

  • Here is the finsihed java program, it just needs to be converted to MIPs assembly language:...

    Here is the finsihed java program, it just needs to be converted to MIPs assembly language: import java.util.*; public class LoveCS { public static void main(String[] args) { int timesMessagePrinted; int count = 0; int sum = 0; Scanner input = new Scanner(System.in); System.out.print("How many times should the message be printed?: "); timesMessagePrinted = input.nextInt(); for(int x = 1; x <= timesMessagePrinted; x++) { System.out.println(x +" I love Computer Science!!"); count++; sum += x; } System.out.print("Printed this message " +count...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

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
ADVERTISEMENT