Question

Write a program to create a file named integerFile.txt if it does not exist. Write 100...

Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order

This is what I have so far:

import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;

public class integerFile {
    public static void main (String[] args) throws Exception{
        Scanner input = new Scanner(System.in);
        File file = new File("integerFile.txt");

       try (PrintWriter pw = new PrintWriter(new PrintWriter(file));) {
            for (int i = 0; i < 100; i++) {
                pw.print((int)(Math.random() * 100) + " ");
            }
            pw.close();
        }
        while(input.hasNext()){
           
        }
    }
}

Help finish in Java

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

import java.io.*;

import java.util.Arrays;

import java.util.Random;

public class FileOperations

{

public static void main(String args[]) throws IOException

{

int randomNumber=0;

try

{

//Java BufferedWriter class is used to write the text to the file

BufferedWriter bw= new BufferedWriter(new FileWriter("D:\\file.txt"));

Random rand= new Random();

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

{

//random number generator between 0-100 with a space

randomNumber= rand.nextInt(101);

bw.write(Integer.toString(randomNumber));

bw.write(" ");

}

bw.close();

String line;

int[] array= new int[100];

//Java BufferedReader class is used to read the text from a character-based input stream.

BufferedReader br= new BufferedReader(new FileReader("D:\\file.txt"));

while((line= br.readLine())!=null)

{

//split function to spilt the text based on a space

String[] numbers =line.split(" ");

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

{

array[i]= Integer.parseInt(numbers[i]);

}

//sort array in increasing order

Arrays.sort(array);

System.out.printf("Sorted array in increasing order");

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

{

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

}

}

}

catch (Exception e) {

// TODO: handle exception

}

}

}

1 import java.io. 2 import j ys; 3 import java.uti1. Random; ava.util.Arra 5 public class FileOperations 7Θ public static voi//Java BufferedReader class is used to read the text from a character-based input streanm BufferedReader br= new BufferedReadorted array in increasing orderl 2 10 12 13 14 15 16 16 16 17 18 18 18 19 20 21 24 24 24 25 26 29 30 30 30

Add a comment
Know the answer?
Add Answer to:
Write a program to create a file named integerFile.txt if it does not exist. Write 100...
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
  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

    **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by white space. Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself. So, if the contents of indata1 were "37 6 90" and the contents of...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Java Write a pair of serialization / deserialization apps that create a user’s Car list, w/...

    Java Write a pair of serialization / deserialization apps that create a user’s Car list, w/ a class named Car, which includes model, VINumber (int), and CarMake (an enum, with valid values FORD, GM, TOYOTA, and HONDA) as fields. Use an array similar to the way the SerializeObjects did to handle several BankAccounts. Your app must include appropriate Exception Handling via try catch blocks (what if the file is not found, or the user inputs characters instead of digits for...

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