1. a) Write a program to create two files with the names file4.txt and file4a.txt two 10 element int arrays with the names arrfile4 and arrfile4a; these arrays are to have different values write the contents of arrfile4 to file4.txt write the contents of arrfile4a to file4a.txt b. Modify exampleFour to read integer values from the keyboard, store them into a 15 element array, then write the array into a file named userin.txt
1. a) Write a program to create
two files with the names file4.txt and file4a.txt
two 10 element int arrays with the names arrfile4 and arrfile4a; these
arrays are to have different values
write the contents of arrfile4 to file4.txt
write the contents of arrfile4a to file4a.txt
b.
Modify exampleFour to read integer values from the keyboard, store them
into a 15 element array, then write the array into a file named userin.txt
exampleFour
In the while loop, this program reads fileLoop.txt until all items are read;
Each item in the file is stored into an array
package storeintoarr;
import java.util.Scanner;
import java.io.*;
public class Storeintoarr {
public static void main(String[] args) throws IOException
{
File loopfile = new File("fileLoop.txt");
Scanner getAll = new Scanner( loopfile );
// connect a Scanner to the file
int num = 0, square = 0;
int[] items = new int[10];
int i = 0;
while(getAll.hasNextInt()){
num = getAll.nextInt();
square = num * num ;
//System.out.println("The square of " + num + " is " + square);
items[i] = num;
System.out.println("items[i] is " + items[i]);
i = i + 1;
}
getAll.close();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images