Question

Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al 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 smalest to the largest. For example if data.txt contains 18 5five 10 1.5 2 2.0 20 Then your program should print to the screen 10 10 20 f the file does not exist, then your program should instead print Fie not found and exit. Your program should start by allocating an array of size 8 and NI it with the fhrst 8 tokens. On the ninth token, it should replace the array with one of sze 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 ntegers already read. Etc. Since this doubling the size of an array is a repeated operation that is easly 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 Bauthor Your Nome (your chosen 4-digit ID) version date of suberission public class Module1Progran Allocates and returns an integer array twice the size of the one supplied as a paraneter. The first half of the new array will array will be zeros Eparom arr the array to be copied + be a copy of the supplied array and the second half of the new Breturn array twice the size of <ttorr/tts with its initial elements copied fron ettsarre/tts ethrows NullPointerException if <ttarrttb is null public static int doubleArrayAndCopyCintarr) public static void main(String args) int data-new int[8]: data doubleArrayAndCopy (data);

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

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Scanner;

public class Module1Program {

public static void main(String[] args) {

int size = 8;

int arr[] = new int[size];

File file = new File("input.txt");

Scanner sc;

try {

sc = new Scanner(file);

} catch (FileNotFoundException e) {

System.out.println("File does not exist");

return;

}

int index = 0;

// reads data from file

while (sc.hasNextLine()) {

int i = sc.nextInt();

arr[index++] = i;

if (index >= size) {

arr = doubleSize(arr, size);

size = size * 2;

}

//System.out.println(i);

}

Arrays.sort(arr); // sort the array

// prints array elements

for (int i = size-index; i < size; i++)

System.out.println(arr[i]);

sc.close();

}

// doubles the array size and copies all old elements to new array

private static int[] doubleSize(int[] aArr, int aSize) {

int temp[] = new int[aSize * 2];

for (int i = 0; i < aArr.length; i++)

temp[i] = aArr[i];

return temp;

}

}

貝Console X <terminated > Module! Program Java Application] CAProgram FilesUavayre1.80-144\binjavaw.exe (Sep ^ 11 2 5 3 7 6 4 21 34 45 32 65 46 87 15 19 32 21 15 19 21 21 32 32 45 65 87 2

Add a comment
Know the answer?
Add Answer to:
Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from...
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
  • c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...

    c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...

  • Java language: C Write a game program that allows the user to collect 4 tokens hidden...

    Java language: C Write a game program that allows the user to collect 4 tokens hidden in an array (cells) of size 10. The program asks the user to select a cell. Determine if there is a token in that cell or not, and give the user appropriate feedback. This program should keep letting user to select until the all tokens are collected. The program should display the total number of selections. Use rand_position Example input/output: There are four tokens....

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a...

    Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a pre-defined text file. A: Text file name should be data.txt B: Text file should be in the same workspace directory of your project's folder C: Text file name should be STORED in a String and called: String strFile ='./data.txt' Defines a 2-D array of integers with dimensions of 5 rows by 5 columns. Inserts the data from the text file to the 2-D array...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • Write a program that reads a file named input.txt and writes a file that contains the...

    Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...

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