Question
Write a complete Java program in a file called Module1Program.java that reads all the tokens from a file named data.txt that is to be found
Write a complete Java program in a file called Module1Program.java that reads all the tokens from a file named data.txt that is to be found in the same directory as the running program. The program should ignore all tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smallest to the largest. For example if data.txt contains
10 5five 10 1.5 2 2.0 20
Then your program should print to the screen
2 10 10 20
If the file does not exist, then your program should instead print "File not found" and exit.
Your program should start by allocating an array of size 8 and fill it with the first 8 tokens. On the ninth token, it should replace the array with one of size 16 and copy all the integers already read. On the 17th token, it should replace the array with one of size 32 and copy all the integers already read. Etc. Since this doubling the size of an array is a repeated operation that is easily factored into a coherent chunk, you should write a method that performs this task.
A little starter code:
/** * Program that reads tokens from file data.txt and prints the integer * tokens found in it to the screen in increasing order. * * @author Your Name * @version date of submission */ public class Module1Program { /** * Allocates and returns an integer array twice the size of the one * supplied as a parameter. The first half of the new array will * be a copy of the supplied array and the second half of the new * array will be zeros. * * @param arr the array to be copied * @return array twice the size of arr with its initial * elements copied from arr * @throws NullPointerException if arr is null. */ public static int[] doubleArrayAndCopy(int[] arr) { } public static void main(String[] args) { int[] data = new int[8]; ... data = doubleArrayAndCopy(data); ... } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started