Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;// importing all the classes in util package class task
- public class Assignment3 {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner str = new Scanner(System.in);
- System.out.println("Enter the Team Name:");
- System.out.println("-->");
- String teamName= str.next();
- int st=0;
- //initiation of all the variables
- String HomeTeam ="";
- String awayTeam="";
- int homeScore=0;
- int awayScore=0;
- String error="";
- Scanner filename = new Scanner(System.in);//a Scanner object for input is created Scanner Yo = new Scanner(System.in);
- System.out.println("enter the file name with file extension:");
- System.out.print("-->");
- String textfilename= filename.next();
- //bufferreader object for reading file
- BufferedReader br = new BufferedReader(new FileReader(textfilename));
- //the title of table
- System.out.println("HomeTeam\tAwayTeam\tHome-Score\tAway-Score");
- //variables for scanning a line of string
- String readLine = null;
- try {
- //reading line to the end of file
- while((readLine = br.readLine()) != null){
- long count = readLine.chars().filter(ch -> ch == ':').count();//count the delimeter
- if(count<3) { //if it is less then 3 program is paused
- error="the match.txt is not a valid file or some charecter is missing from file";
- break;
- }
- String [] game = readLine.split(":"); //spliting the line using delimiter
- //storing the text data to program variables
- HomeTeam=game[0];
- if(HomeTeam.length()==0){//if length is 0 it means it is empty ,this program will be halted
- error="Home team name may be missing ";
- break;
- }
- awayTeam=game[1];
- if(awayTeam.length()==0){//if length is 0 it means it is empty ,this program will be halted
- error="Away team name may be missing ";
- break;
- }
- if(game[2].length()==0){//if length is 0 it means it is empty ,this program will be halted
- error="Home Team Score May be Missing";
- break;
- }
- try{
- homeScore=Integer.parseInt(game[2]);//parsing to integer form, if failed while parsing program is exit
- }catch(NumberFormatException ex){
- System.out.println("The Home Team Score is not a valid Integer ");
- break;
- }
- if(game[2].length()==0){//if length is 0 it means it is empty ,this program will be halted
- error="Away Team Score May be Missing";
- break;
- }
- try{
- awayScore=Integer.parseInt(game[3]);//parsing to integer form, if failed while parsing program is exit
- }catch(NumberFormatException ex){
- System.out.println("The Away Team Score is not a valid Integer ");
- break;
- }
- //the outputs
- if(teamName.equals(HomeTeam)){
- System.out.print(HomeTeam);
- System.out.println("\t"+"\t"+homeScore);
- st=1;
- }else if(teamName.equals(awayTeam)){
- System.out.print("\t"+"\t"+"\t"+"\t"+awayTeam);
- System.out.println("\t"+"\t"+awayScore);
- st=1;
- }
- }
- if(st==0){
- clear();
- error="No team found!";
- }
- } catch (IOException ex) {//shows error while reading files
- }
- System.out.println(error); //printing error
- }
- static void clear(){//clear screen
- for(int i=0; i<1000;i++){
- System.out.println("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement