Write a main method similar to the one below, but with the following differences
ID: 3692790 • Letter: W
Question
Write a main method similar to the one below, but with the following differences. Inside the curly braces of the main method, delcare an array of references to String objets. You'll need Java code that looks something like this:
final int MAX = 100;
String [] lines = new String[MAX];
------------------------------------------------------------------------------------------------------
//displays the contents of the file quotations.txt. on the screen
import java.util.*;
import java.io.*;
public class File1
{
public static void main(String[] args) throws IOException
{
File inputFile = new File("quotations.txt.");
if (!inputFile.exists())
{
System.out.println("File quotations.txt not found.");
System.exit(0);
}
Scanner input = new Scanner(inputFile);
String line; // to hold one full line from the file
while (input.hasNext()) // while there is more data
{
line = input.nextLine(); //advance to next liine, returns all "skipped" data
System.out.println(line);
}
input.close();
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
public class File1
{
public static void main(String[] args) throws IOException
{
final int MAX = 100;
String [] lines = new String[MAX];
File inputFile = new File("quotations.txt.");
if (!inputFile.exists())
{
System.out.println("File quotations.txt not found.");
System.exit(0);
}
Scanner input = new Scanner(inputFile);
// to hold one full line from the file
int i=0;
while (input.hasNext()) // while there is more data
{
lines[i] = input.nextLine(); //advance to next liine, returns all "skipped" data
System.out.println(lines[i++]);
}
input.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.