To begin, download the text file and save it in the same folder as your java sou
ID: 3762455 • Letter: T
Question
To begin, download the text file and save it in the same folder as your java source files: http://courses.cs.tamu.edu/hurley/cpsc111/homework/SuperBowlWinners.txt
Next, write a program that opens and reads the data from this file into a 2D array. Each row of your array corresponds to one line of the file. The first column should contain the year and the second column the full team name. Then ask the user to enter a year, and output the team that won the SuperBowl that year. Give an error if the year is not in the data. Let the user keep entering a year until they enter STOP.
Explanation / Answer
import java.util.*;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
class Test
{
public static void main(String[] args)
{
int count1=0,count=0,i=0;
BufferedReader br=null;
BufferedReader b=null;
try{
br=new BufferedReader(new FileReader("SuperBowlWinners.txt"));
}
catch(Exception e)
{
System.out.println("File not found");
}
try {
while(br.readLine()!=null)
{
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
String array[][]=new String[count][2];
try {
b=new BufferedReader(new FileReader("SuperBowlWinners.txt"));
String line=b.readLine();
while(line!=null)
{
array[i][0]=line.substring(0, line.indexOf(" "));
array[i][1]=line.substring(line.indexOf(' '));
i++;
line=b.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
String input;
System.out.println("Enter a year");
Scanner s=new Scanner(System.in);
input=s.next();
while(!(input.equals("STOP")))
{
count1=0;
for(int j=0;j<array.length;j++)
{
if(input.equals(array[j][0]))
{
System.out.println(array[j][1]);
break;
}
else
count1++;
}
if(count1==array.length)
{
System.out.println("Error. Data not Found");
}
System.out.println("Enter a year");
input=s.next();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.