File Input Lab Read the first 10,000 words in the book “Journey to the Center of
ID: 3827903 • Letter: F
Question
File Input Lab
Read the first 10,000 words in the book “Journey to the Center of the Earth” (included with the lab) into a String array of size 10,000 in the main method. Write a method called evenWords, which gets all the even words and puts them into an array of size 5000 and returns the array. Back in main print out the last 100 words in the array.
I have the .txt file on hand and this has to be done in java oracle under one class
EDIT: Here is what I have so far
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileInputLab {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File myFile = new File("C:/Users/Richie/Documents/workspace/LastLab/src/textFile.txt");
Scanner inputFile = new Scanner(myFile);
String[] myArray = new String[10000];
for(int i = 0; i<10000; i++){
myArray[i] = inputFile.next();
}
}
}
Explanation / Answer
Here is code:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileInputLab {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File myFile = new File("C:/Users/Venkat/Documents/NetBeansProjects/JavaApplication1/src/javaapplication1/Chegg22/EmployeeInfo.dat");
Scanner inputFile = new Scanner(myFile);
String[] myArray = new String[10000];
for(int i = 0; i<10000; i++){
myArray[i] = inputFile.next();
}
String[] eWords = evenWords(myArray);
// print the back 100 in eWords array
for (int i = 4999; i > (4999 - 100); i--) {
System.out.println((5000 - i) + " " + eWords[i]);
}
}
public static String[] evenWords(String[] str)
{
String[] myArray = new String[5000];
int count = 0;
// read the words untill 5000
for (int i = 0; i < str.length && i < 10000 ; i++) {
if(i%2 == 0) // checks if i is even
{
myArray[count] = str[i];
count++;
}
}
return myArray;
}
}
Output:
1 a
2 then.
3 dramatically
4 continuously
5 has
6 of
7 and
8 speed,
9 II.
10 World
11 developed
12 machines
13 electronic
14 first
15 century.
16 early
17 in
18 analog
19 did
20 electrical
21 More
22 for
23 guiding
24 such
25 tedious
26 automate
27 built
28 devices
29 some
30 Industrial
31 in
32 calculations.
33 in
34 aided
35 the
36 devices
37 simple
38 ancient
39 computers.
40 of
41 connects
42 and
43 on
44 is
45 The
46 as
47 devices
48 and
49 personal
50 devices
51 general
52 also
53 design,
54 computer
55 robots
56 as
57 devices
58 controls,
59 and
60 microwave
61 devices
62 special
63 includes
64 devices.
65 and
66 of
67 wide
68 a
69 systems
70 as
71 are
72 Such
73 of
74 wide
75 to
76 very
77 make
78 a
79 operations,
80 sequence
81 follow
82 computers
83 ability
84 automatically.
85 logical
86 arithmetic
87 set
88 an
89 carry
90 instructed
91 can
92 device
93 is
94 A
95 family
96 on
97 instead
98 and
99 of
100 for
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.