Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming. Create a program that will open a file named numbers.txt. This

ID: 3806986 • Letter: J

Question

Java programming.

Create a program that will open a file named numbers.txt. This will be a file that will contain one integer number on each line. Create a 100 element array. After opening the file, your program will read each number from the fileand store the numbers into the array. The integers in the file can be any valid integer including positive numbers, negative numbers, and zero. After storingallthe numbers intothe array, create a loop that will cycle through the array and printout the numbers to the screen, one number per line (use the System.out.println method). Your loop is to loop only enough times to printout the numbers read into the array.You will not know how many numbers are in the file, so you need to code your program to work with files that may contain any number of numbers.

Explanation / Answer

import java.util.Scanner;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
try
{
// opening file
Scanner scanner = new Scanner(new File("numbers.txt"));
int [] a = new int [100];
int i = 0;
// reading as long as there are elements in file
while(scanner.hasNextInt())
{
a[i++] = scanner.nextInt();
}
int count = i, temp,j;
  
// sorting the array
for (i = 0; i < count; i++)
{
for (j = i + 1; j < count; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
  
// printing the array
for(j=0;j<count;j++)
{
System.out.printf("%d ",a[j]);
}
  
}
catch(Exception e)
{}
}

}

/*

Sample File : Numbers.txt

123 -12 0 12
34 67 124

Sample Output

-12 0 12 34 67 123 124

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote